Skip to content

Commit b754fa2

Browse files
committed
feat: add Creational folder
1 parent 2141326 commit b754fa2

23 files changed

+230
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func createOutlets(on factory: AbstractFactory) -> (chair: Chair, table: Table, sofa: Sofa) {
2+
let chair = factory.createChair()
3+
let table = factory.createTable()
4+
let sofa = factory.createSofa()
5+
6+
return (chair, table, sofa)
7+
}
8+
9+
let kitchenOutlets = createOutlets(on: KitchenFactory())
10+
kitchenOutlets.chair
11+
kitchenOutlets.table
12+
kitchenOutlets.sofa
13+
14+
let bedRoomOutlets = createOutlets(on: BedroomFactory())
15+
bedRoomOutlets.chair
16+
bedRoomOutlets.table
17+
bedRoomOutlets.sofa
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
public protocol AbstractFactory {
4+
func createChair() -> Chair
5+
func createTable() -> Table
6+
func createSofa() -> Sofa
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct BedroomChair: Chair {
2+
public var name: String = "Chair"
3+
public var type: String = "Chair for bedroom"
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public struct BedroomFactory: AbstractFactory {
2+
// MARK: - Init
3+
4+
public init() { }
5+
6+
// MARK: - AbstractFactory
7+
8+
public func createChair() -> Chair {
9+
print("Bedroom chair created")
10+
return BedroomChair()
11+
}
12+
13+
public func createTable() -> Table {
14+
print("Bedroom table created")
15+
return BedroomTable()
16+
}
17+
18+
public func createSofa() -> Sofa {
19+
print("Bedroom sofa created")
20+
return BedroomSofa()
21+
}
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct BedroomSofa: Sofa {
2+
public var name: String = "Sofa"
3+
public var type: String = "Sofa for bedroom"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct BedroomTable: Table {
2+
public var name: String = "Table"
3+
public var type: String = "Table for bedroom"
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public protocol Chair {
2+
// MARK: - Properties
3+
4+
var name: String { get }
5+
var type: String { get }
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct KitchenChair: Chair {
2+
public var name: String = "Chair"
3+
public var type: String = "Chair for kitchen"
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public struct KitchenFactory: AbstractFactory {
2+
// MARK: - Init
3+
4+
public init() { }
5+
6+
// MARK: - AbstractFactory
7+
8+
public func createChair() -> Chair {
9+
print("Kitchen chair created")
10+
return KitchenChair()
11+
}
12+
13+
public func createTable() -> Table {
14+
print("Kitchen table created")
15+
return KitchenTable()
16+
}
17+
18+
public func createSofa() -> Sofa {
19+
print("Kitchen sofa created")
20+
return KitchenSofa()
21+
}
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct KitchenSofa: Sofa {
2+
public var name: String = "Sofa"
3+
public var type: String = "Sofa for ktichen"
4+
}

0 commit comments

Comments
 (0)