Skip to content

Commit 921e7f0

Browse files
author
Oktawian Chojnacki
committed
Generated after merge.
1 parent ffc60e9 commit 921e7f0

File tree

6 files changed

+98
-108
lines changed

6 files changed

+98
-108
lines changed

Design-Patterns.playground.zip

-2 Bytes
Binary file not shown.

Design-Patterns.playground/Pages/Behavioral.xcplaygroundpage/Contents.swift

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ class MoneyPile {
2222
let value: Int
2323
var quantity: Int
2424
var nextPile: MoneyPile?
25-
25+
2626
init(value: Int, quantity: Int, nextPile: MoneyPile?) {
2727
self.value = value
2828
self.quantity = quantity
2929
self.nextPile = nextPile
3030
}
31-
31+
3232
func canWithdraw(var v: Int) -> Bool {
3333

3434
func canTakeSomeBill(want: Int) -> Bool {
3535
return (want / self.value) > 0
3636
}
37-
37+
3838
var q = self.quantity
3939

4040
while canTakeSomeBill(v) {
@@ -62,22 +62,22 @@ class ATM {
6262
private var fifty: MoneyPile
6363
private var twenty: MoneyPile
6464
private var ten: MoneyPile
65-
65+
6666
private var startPile: MoneyPile {
6767
return self.hundred
6868
}
69-
70-
init(hundred: MoneyPile,
71-
fifty: MoneyPile,
72-
twenty: MoneyPile,
69+
70+
init(hundred: MoneyPile,
71+
fifty: MoneyPile,
72+
twenty: MoneyPile,
7373
ten: MoneyPile) {
7474

7575
self.hundred = hundred
7676
self.fifty = fifty
7777
self.twenty = twenty
7878
self.ten = ten
7979
}
80-
80+
8181
func canWithdraw(value: Int) -> String {
8282
return "Can withdraw: \(self.startPile.canWithdraw(value))"
8383
}
@@ -115,7 +115,7 @@ class OpenCommand : DoorCommand {
115115
required init(doors: String) {
116116
self.doors = doors
117117
}
118-
118+
119119
func execute() -> String {
120120
return "Opened \(doors)"
121121
}
@@ -127,7 +127,7 @@ class CloseCommand : DoorCommand {
127127
required init(doors: String) {
128128
self.doors = doors
129129
}
130-
130+
131131
func execute() -> String {
132132
return "Closed \(doors)"
133133
}
@@ -136,16 +136,16 @@ class CloseCommand : DoorCommand {
136136
class HAL9000DoorsOperations {
137137
let openCommand: DoorCommand
138138
let closeCommand: DoorCommand
139-
139+
140140
init(doors: String) {
141141
self.openCommand = OpenCommand(doors:doors)
142142
self.closeCommand = CloseCommand(doors:doors)
143143
}
144-
144+
145145
func close() -> String {
146146
return closeCommand.execute()
147147
}
148-
148+
149149
func open() -> String {
150150
return openCommand.execute()
151151
}
@@ -175,35 +175,35 @@ protocol IntegerExp {
175175

176176
class IntegerContext {
177177
private var data: [Character:Int] = [:]
178-
178+
179179
func lookup(name: Character) -> Int {
180180
return self.data[name]!
181181
}
182-
182+
183183
func assign(integerVarExp: IntegerVarExp, value: Int) {
184184
self.data[integerVarExp.name] = value
185185
}
186186
}
187187

188188
class IntegerVarExp: IntegerExp {
189189
let name: Character
190-
190+
191191
init(name: Character) {
192192
self.name = name
193193
}
194-
194+
195195
func evaluate(context: IntegerContext) -> Int {
196196
return context.lookup(self.name)
197197
}
198-
198+
199199
func replace(name: Character, integerExp: IntegerExp) -> IntegerExp {
200200
if name == self.name {
201201
return integerExp.copy()
202202
} else {
203203
return IntegerVarExp(name: self.name)
204204
}
205205
}
206-
206+
207207
func copy() -> IntegerExp {
208208
return IntegerVarExp(name: self.name)
209209
}
@@ -212,21 +212,21 @@ class IntegerVarExp: IntegerExp {
212212
class AddExp: IntegerExp {
213213
private var operand1: IntegerExp
214214
private var operand2: IntegerExp
215-
215+
216216
init(op1: IntegerExp, op2: IntegerExp) {
217217
self.operand1 = op1
218218
self.operand2 = op2
219219
}
220-
220+
221221
func evaluate(context: IntegerContext) -> Int {
222222
return self.operand1.evaluate(context) + self.operand2.evaluate(context)
223223
}
224-
224+
225225
func replace(character: Character, integerExp: IntegerExp) -> IntegerExp {
226226
return AddExp(op1: operand1.replace(character, integerExp: integerExp),
227227
op2: operand2.replace(character, integerExp: integerExp))
228228
}
229-
229+
230230
func copy() -> IntegerExp {
231231
return AddExp(op1: self.operand1, op2: self.operand2)
232232
}
@@ -262,7 +262,7 @@ struct NovellasCollection<T> {
262262

263263
extension NovellasCollection: SequenceType {
264264
typealias Generator = AnyGenerator<T>
265-
265+
266266
func generate() -> AnyGenerator<T> {
267267
var i = 0
268268
return anyGenerator{ return i >= self.novellas.count ? nil : self.novellas[i++] }
@@ -287,15 +287,15 @@ The mediator pattern is used to reduce coupling between classes that communicate
287287

288288
class Colleague {
289289
let mediator: Mediator
290-
290+
291291
init(mediator: Mediator) {
292292
self.mediator = mediator
293293
}
294-
294+
295295
func send(message: String) {
296296
mediator.send(message, colleague: self)
297297
}
298-
298+
299299
func receive(message: String) {
300300
assert(false, "Method should be overriden")
301301
}
@@ -307,11 +307,11 @@ protocol Mediator {
307307

308308
class MessageMediator: Mediator {
309309
private var colleagues: [Colleague] = []
310-
310+
311311
func addColleague(colleague: Colleague) {
312312
colleagues.append(colleague)
313313
}
314-
314+
315315
func send(message: String, colleague: Colleague) {
316316
for c in colleagues {
317317
if c !== colleague { //for simplicity we compare object references
@@ -411,7 +411,7 @@ gameState.restoreFromMemento(CheckPoint.restorePreviousState(keyName: "gameState
411411
👓 Observer
412412
-----------
413413

414-
The observer pattern is used to allow an object to publish changes to its state.
414+
The observer pattern is used to allow an object to publish changes to its state.
415415
Other objects subscribe to be immediately notified of any changes.
416416

417417
### Example
@@ -459,7 +459,7 @@ testChambers.testChamberNumber++
459459
🐉 State
460460
---------
461461

462-
The state pattern is used to alter the behaviour of an object as its internal state changes.
462+
The state pattern is used to alter the behaviour of an object as its internal state changes.
463463
The pattern allows the class for an object to apparently change at run-time.
464464

465465
### Example
@@ -482,7 +482,7 @@ class Context {
482482
func changeStateToUnauthorized() {
483483
state = UnauthorizedState()
484484
}
485-
485+
486486
}
487487

488488
protocol State {
@@ -529,11 +529,11 @@ protocol PrintStrategy {
529529
class Printer {
530530

531531
let strategy: PrintStrategy
532-
532+
533533
func printString(string: String) -> String {
534534
return self.strategy.printString(string)
535535
}
536-
536+
537537
init(strategy: PrintStrategy) {
538538
self.strategy = strategy
539539
}

Design-Patterns.playground/Pages/Creational.xcplaygroundpage/Contents.swift

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,42 @@ import Foundation/*:
1414
🌰 Abstract Factory
1515
-------------------
1616

17-
The abstract factory pattern is used to provide a client with a set of related or dependant objects.
17+
The abstract factory pattern is used to provide a client with a set of related or dependant objects.
1818
The "family" of objects created by the factory are determined at run-time.
1919

2020
### Example
2121
*/
22-
/*:
22+
/*:
2323
Protocols
2424
*/
2525
protocol Decimal {
2626
func stringValue() -> String
27+
// factory
28+
static func make(string : String) -> Decimal
2729
}
2830

29-
protocol NumberFactoryProtocol {
30-
func numberFromString(string : String) -> Decimal
31-
}
31+
typealias NumberFactory = (String) -> Decimal
3232

33-
// Number implementations
33+
// Number implementations with factory methods
3434

3535
struct NextStepNumber : Decimal {
3636
private var nextStepNumber : NSNumber
3737

3838
func stringValue() -> String { return nextStepNumber.stringValue }
39+
40+
// factory
41+
static func make(string : String) -> Decimal {
42+
return NextStepNumber(nextStepNumber:NSNumber(longLong:(string as NSString).longLongValue))
43+
}
3944
}
4045

4146
struct SwiftNumber : Decimal {
4247
private var swiftInt : Int
4348

4449
func stringValue() -> String { return "\(swiftInt)" }
45-
}
46-
/*:
47-
Factories
48-
*/
49-
class NextStepNumberFactory : NumberFactoryProtocol {
50-
func numberFromString(string : String) -> Decimal {
51-
return NextStepNumber(nextStepNumber:NSNumber(longLong:(string as NSString).longLongValue))
52-
}
53-
}
54-
55-
class SwiftNumberFactory : NumberFactoryProtocol {
56-
func numberFromString(string : String) -> Decimal {
50+
51+
// factory
52+
static func make(string : String) -> Decimal {
5753
return SwiftNumber(swiftInt:(string as NSString).integerValue)
5854
}
5955
}
@@ -64,32 +60,31 @@ enum NumberType {
6460
case NextStep, Swift
6561
}
6662

67-
class NumberAbstractFactory {
68-
class func numberFactoryType(type : NumberType) -> NumberFactoryProtocol {
69-
63+
class NumberHelper {
64+
class func factoryFor(type : NumberType) -> NumberFactory {
7065
switch type {
71-
case .NextStep:
72-
return NextStepNumberFactory()
73-
case .Swift:
74-
return SwiftNumberFactory()
66+
case .NextStep:
67+
return NextStepNumber.make
68+
case .Swift:
69+
return SwiftNumber.make
7570
}
7671
}
7772
}
7873
/*:
7974
### Usage
8075
*/
81-
let factoryOne = NumberAbstractFactory.numberFactoryType(.NextStep)
82-
let numberOne = factoryOne.numberFromString("1")
76+
let factoryOne = NumberHelper.factoryFor(.NextStep)
77+
let numberOne = factoryOne("1")
8378
numberOne.stringValue()
8479

85-
let factoryTwo = NumberAbstractFactory.numberFactoryType(.Swift)
86-
let numberTwo = factoryTwo.numberFromString("2")
80+
let factoryTwo = NumberHelper.factoryFor(.Swift)
81+
let numberTwo = factoryTwo("2")
8782
numberTwo.stringValue()
8883
/*:
8984
👷 Builder
9085
----------
9186

92-
The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm.
87+
The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm.
9388
An external class controls the construction algorithm.
9489

9590
### Example
@@ -155,7 +150,7 @@ class Euro : Currency {
155150
func symbol() -> String {
156151
return ""
157152
}
158-
153+
159154
func code() -> String {
160155
return "EUR"
161156
}
@@ -165,7 +160,7 @@ class UnitedStatesDolar : Currency {
165160
func symbol() -> String {
166161
return "$"
167162
}
168-
163+
169164
func code() -> String {
170165
return "USD"
171166
}
@@ -186,7 +181,7 @@ class CurrencyFactory {
186181
default:
187182
return nil
188183
}
189-
184+
190185
}
191186
}
192187
/*:
@@ -202,7 +197,7 @@ CurrencyFactory.currencyForCountry(.UK)?.code() ?? noCurrencyCode
202197
🃏 Prototype
203198
------------
204199

205-
The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone.
200+
The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone.
206201
This practise is particularly useful when the construction of a new object is inefficient.
207202

208203
### Example

0 commit comments

Comments
 (0)