Skip to content

Commit e7e840c

Browse files
author
Oktawian Chojnacki
committed
Abstract Factory fixed.
1 parent 7ccca81 commit e7e840c

File tree

4 files changed

+101
-36
lines changed

4 files changed

+101
-36
lines changed

Design-Patterns.playground.zip

269 Bytes
Binary file not shown.
Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
1-
class Number {
2-
var number: AnyObject
1+
// Protocols.
32

4-
init(number:AnyObject) {
5-
self.number = number
6-
}
3+
protocol Decimal {
4+
func stringValue() -> String
5+
}
76

8-
convenience init(integer: Int) {
9-
self.init(number: integer)
10-
}
7+
protocol NumberFactoryProtocol {
8+
func numberFromString(string : String) -> Decimal
9+
}
1110

12-
convenience init(double: Double) {
13-
self.init(number: double)
14-
}
11+
// Number implementations.
12+
13+
struct NextStepNumber : Decimal {
14+
private var nextStepNumber : NSNumber
15+
func stringValue() -> String { return nextStepNumber.stringValue }
16+
}
17+
18+
struct SwiftNumber : Decimal {
19+
private var swiftInt : Int
20+
func stringValue() -> String { return "\(swiftInt)" }
21+
}
22+
23+
// Factories.
1524

16-
func integerValue() -> Int {
17-
return self.number as Int
25+
class NextStepNumberFactory : NumberFactoryProtocol {
26+
func numberFromString(string : String) -> Decimal {
27+
return NextStepNumber(nextStepNumber:NSNumber(longLong:(string as NSString).longLongValue))
1828
}
29+
}
1930

20-
func doubleValue() -> Double {
21-
return self.number as Double
31+
class SwiftNumberFactory : NumberFactoryProtocol {
32+
func numberFromString(string : String) -> Decimal {
33+
return SwiftNumber(swiftInt:(string as NSString).integerValue)
2234
}
2335
}
36+
37+
// Abstract factory.
38+
39+
enum NumberType {
40+
case NextStep, Swift
41+
}
42+
43+
class NumberAbstractFactory {
44+
class func numberFactoryType(type : NumberType) -> NumberFactoryProtocol {
45+
switch (type) {
46+
case .NextStep:
47+
return NextStepNumberFactory()
48+
case .Swift:
49+
return SwiftNumberFactory()
50+
}
51+
}
52+
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
let number = Number(double: 12.1)
2-
let double = number.doubleValue()
3-
let integer = number.integerValue()
1+
let factoryOne = NumberAbstractFactory.numberFactoryType(.NextStep)
2+
let numberOne = factoryOne.numberFromString("1")
3+
numberOne.stringValue()
4+
5+
let factoryTwo = NumberAbstractFactory.numberFactoryType(.Swift)
6+
let numberTwo = factoryTwo.numberFromString("2")
7+
numberTwo.stringValue()

README.markdown

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,71 @@ let uglierPoint = Point {
8686
##🌰 Abstract Factory
8787

8888
```swift
89-
class Number {
90-
var number: AnyObject
89+
// Protocols.
9190

92-
init(number:AnyObject) {
93-
self.number = number
94-
}
91+
protocol Decimal {
92+
func stringValue() -> String
93+
}
9594

96-
convenience init(integer: Int) {
97-
self.init(number: integer)
98-
}
95+
protocol NumberFactoryProtocol {
96+
func numberFromString(string : String) -> Decimal
97+
}
9998

100-
convenience init(double: Double) {
101-
self.init(number: double)
102-
}
99+
// Number implementations.
100+
101+
struct NextStepNumber : Decimal {
102+
private var nextStepNumber : NSNumber
103+
func stringValue() -> String { return nextStepNumber.stringValue }
104+
}
105+
106+
struct SwiftNumber : Decimal {
107+
private var swiftInt : Int
108+
func stringValue() -> String { return "\(swiftInt)" }
109+
}
103110

104-
func integerValue() -> Int {
105-
return self.number as Int
111+
// Factories.
112+
113+
class NextStepNumberFactory : NumberFactoryProtocol {
114+
func numberFromString(string : String) -> Decimal {
115+
return NextStepNumber(nextStepNumber:NSNumber(longLong:(string as NSString).longLongValue))
106116
}
117+
}
107118

108-
func doubleValue() -> Double {
109-
return self.number as Double
119+
class SwiftNumberFactory : NumberFactoryProtocol {
120+
func numberFromString(string : String) -> Decimal {
121+
return SwiftNumber(swiftInt:(string as NSString).integerValue)
110122
}
111123
}
112124

125+
// Abstract factory.
126+
127+
enum NumberType {
128+
case NextStep, Swift
129+
}
130+
131+
class NumberAbstractFactory {
132+
class func numberFactoryType(type : NumberType) -> NumberFactoryProtocol {
133+
switch (type) {
134+
case .NextStep:
135+
return NextStepNumberFactory()
136+
case .Swift:
137+
return SwiftNumberFactory()
138+
}
139+
}
140+
}
113141
```
114142
**Usage:**
115143
```swift
116-
let number = Number(double: 12.1)
117-
let double = number.doubleValue()
118-
let integer = number.integerValue()
144+
let factoryOne = NumberAbstractFactory.numberFactoryType(.NextStep)
145+
let numberOne = factoryOne.numberFromString("1")
146+
numberOne.stringValue()
119147

148+
let factoryTwo = NumberAbstractFactory.numberFactoryType(.Swift)
149+
let numberTwo = factoryTwo.numberFromString("2")
150+
numberTwo.stringValue()
120151
```
121152

153+
122154
##🃏 Prototype
123155

124156
```swift

0 commit comments

Comments
 (0)