@@ -22,19 +22,19 @@ class MoneyPile {
22
22
let value : Int
23
23
var quantity : Int
24
24
var nextPile : MoneyPile ?
25
-
25
+
26
26
init ( value: Int , quantity: Int , nextPile: MoneyPile ? ) {
27
27
self . value = value
28
28
self . quantity = quantity
29
29
self . nextPile = nextPile
30
30
}
31
-
31
+
32
32
func canWithdraw( var v: Int ) -> Bool {
33
33
34
34
func canTakeSomeBill( want: Int ) -> Bool {
35
35
return ( want / self . value) > 0
36
36
}
37
-
37
+
38
38
var q = self . quantity
39
39
40
40
while canTakeSomeBill ( v) {
@@ -62,22 +62,22 @@ class ATM {
62
62
private var fifty : MoneyPile
63
63
private var twenty : MoneyPile
64
64
private var ten : MoneyPile
65
-
65
+
66
66
private var startPile : MoneyPile {
67
67
return self . hundred
68
68
}
69
-
70
- init ( hundred: MoneyPile ,
71
- fifty: MoneyPile ,
72
- twenty: MoneyPile ,
69
+
70
+ init ( hundred: MoneyPile ,
71
+ fifty: MoneyPile ,
72
+ twenty: MoneyPile ,
73
73
ten: MoneyPile ) {
74
74
75
75
self . hundred = hundred
76
76
self . fifty = fifty
77
77
self . twenty = twenty
78
78
self . ten = ten
79
79
}
80
-
80
+
81
81
func canWithdraw( value: Int ) -> String {
82
82
return " Can withdraw: \( self . startPile. canWithdraw ( value) ) "
83
83
}
@@ -115,7 +115,7 @@ class OpenCommand : DoorCommand {
115
115
required init ( doors: String ) {
116
116
self . doors = doors
117
117
}
118
-
118
+
119
119
func execute( ) -> String {
120
120
return " Opened \( doors) "
121
121
}
@@ -127,7 +127,7 @@ class CloseCommand : DoorCommand {
127
127
required init ( doors: String ) {
128
128
self . doors = doors
129
129
}
130
-
130
+
131
131
func execute( ) -> String {
132
132
return " Closed \( doors) "
133
133
}
@@ -136,16 +136,16 @@ class CloseCommand : DoorCommand {
136
136
class HAL9000DoorsOperations {
137
137
let openCommand : DoorCommand
138
138
let closeCommand : DoorCommand
139
-
139
+
140
140
init ( doors: String ) {
141
141
self . openCommand = OpenCommand ( doors: doors)
142
142
self . closeCommand = CloseCommand ( doors: doors)
143
143
}
144
-
144
+
145
145
func close( ) -> String {
146
146
return closeCommand. execute ( )
147
147
}
148
-
148
+
149
149
func open( ) -> String {
150
150
return openCommand. execute ( )
151
151
}
@@ -175,35 +175,35 @@ protocol IntegerExp {
175
175
176
176
class IntegerContext {
177
177
private var data : [ Character : Int ] = [ : ]
178
-
178
+
179
179
func lookup( name: Character ) -> Int {
180
180
return self . data [ name] !
181
181
}
182
-
182
+
183
183
func assign( integerVarExp: IntegerVarExp , value: Int ) {
184
184
self . data [ integerVarExp. name] = value
185
185
}
186
186
}
187
187
188
188
class IntegerVarExp : IntegerExp {
189
189
let name : Character
190
-
190
+
191
191
init ( name: Character ) {
192
192
self . name = name
193
193
}
194
-
194
+
195
195
func evaluate( context: IntegerContext ) -> Int {
196
196
return context. lookup ( self . name)
197
197
}
198
-
198
+
199
199
func replace( name: Character , integerExp: IntegerExp ) -> IntegerExp {
200
200
if name == self . name {
201
201
return integerExp. copy ( )
202
202
} else {
203
203
return IntegerVarExp ( name: self . name)
204
204
}
205
205
}
206
-
206
+
207
207
func copy( ) -> IntegerExp {
208
208
return IntegerVarExp ( name: self . name)
209
209
}
@@ -212,21 +212,21 @@ class IntegerVarExp: IntegerExp {
212
212
class AddExp : IntegerExp {
213
213
private var operand1 : IntegerExp
214
214
private var operand2 : IntegerExp
215
-
215
+
216
216
init ( op1: IntegerExp , op2: IntegerExp ) {
217
217
self . operand1 = op1
218
218
self . operand2 = op2
219
219
}
220
-
220
+
221
221
func evaluate( context: IntegerContext ) -> Int {
222
222
return self . operand1. evaluate ( context) + self . operand2. evaluate ( context)
223
223
}
224
-
224
+
225
225
func replace( character: Character , integerExp: IntegerExp ) -> IntegerExp {
226
226
return AddExp ( op1: operand1. replace ( character, integerExp: integerExp) ,
227
227
op2: operand2. replace ( character, integerExp: integerExp) )
228
228
}
229
-
229
+
230
230
func copy( ) -> IntegerExp {
231
231
return AddExp ( op1: self . operand1, op2: self . operand2)
232
232
}
@@ -262,7 +262,7 @@ struct NovellasCollection<T> {
262
262
263
263
extension NovellasCollection : SequenceType {
264
264
typealias Generator = AnyGenerator < T >
265
-
265
+
266
266
func generate( ) -> AnyGenerator < T > {
267
267
var i = 0
268
268
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
287
287
288
288
class Colleague {
289
289
let mediator : Mediator
290
-
290
+
291
291
init ( mediator: Mediator ) {
292
292
self . mediator = mediator
293
293
}
294
-
294
+
295
295
func send( message: String ) {
296
296
mediator. send ( message, colleague: self )
297
297
}
298
-
298
+
299
299
func receive( message: String ) {
300
300
assert ( false , " Method should be overriden " )
301
301
}
@@ -307,11 +307,11 @@ protocol Mediator {
307
307
308
308
class MessageMediator : Mediator {
309
309
private var colleagues : [ Colleague ] = [ ]
310
-
310
+
311
311
func addColleague( colleague: Colleague ) {
312
312
colleagues. append ( colleague)
313
313
}
314
-
314
+
315
315
func send( message: String , colleague: Colleague ) {
316
316
for c in colleagues {
317
317
if c !== colleague { //for simplicity we compare object references
@@ -411,7 +411,7 @@ gameState.restoreFromMemento(CheckPoint.restorePreviousState(keyName: "gameState
411
411
👓 Observer
412
412
-----------
413
413
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.
415
415
Other objects subscribe to be immediately notified of any changes.
416
416
417
417
### Example
@@ -459,7 +459,7 @@ testChambers.testChamberNumber++
459
459
🐉 State
460
460
---------
461
461
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.
463
463
The pattern allows the class for an object to apparently change at run-time.
464
464
465
465
### Example
@@ -482,7 +482,7 @@ class Context {
482
482
func changeStateToUnauthorized( ) {
483
483
state = UnauthorizedState ( )
484
484
}
485
-
485
+
486
486
}
487
487
488
488
protocol State {
@@ -529,11 +529,11 @@ protocol PrintStrategy {
529
529
class Printer {
530
530
531
531
let strategy : PrintStrategy
532
-
532
+
533
533
func printString( string: String ) -> String {
534
534
return self . strategy. printString ( string)
535
535
}
536
-
536
+
537
537
init ( strategy: PrintStrategy ) {
538
538
self . strategy = strategy
539
539
}
0 commit comments