Skip to content

Commit dc27931

Browse files
committed
Merge pull request ochococo#57 from kingreza/master
Add further example. update source to match README
2 parents 6b86c20 + 2d7c6a4 commit dc27931

File tree

19 files changed

+111
-22
lines changed

19 files changed

+111
-22
lines changed

Design-Patterns.playground.zip

5.09 KB
Binary file not shown.

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

+24-4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ atm.canWithdraw(100) // Can withdraw - 1x100
9999
atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value of 5
100100
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
101101
/*:
102+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
103+
*/
104+
/*:
102105
👫 Command
103106
----------
104107

@@ -250,6 +253,9 @@ intContext.assign(c, value: 3)
250253

251254
var result = expression?.evaluate(intContext)
252255
/*:
256+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
257+
*/
258+
/*:
253259
🍫 Iterator
254260
-----------
255261

@@ -342,6 +348,9 @@ messagesMediator.addColleague(user1)
342348

343349
user0.send("Hello") // user1 receives message
344350
/*:
351+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
352+
*/
353+
/*:
345354
💾 Memento
346355
----------
347356

@@ -373,14 +382,14 @@ class GameState {
373382
/*:
374383
Caretaker
375384
*/
376-
class CheckPoint {
377-
class func saveState(memento: Memento, keyName: String = DPMementoGameState) {
385+
enum CheckPoint {
386+
static func saveState(memento: Memento, keyName: String = DPMementoGameState) {
378387
let defaults = NSUserDefaults.standardUserDefaults()
379388
defaults.setObject(memento, forKey: keyName)
380389
defaults.synchronize()
381390
}
382391

383-
class func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
392+
static func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
384393
let defaults = NSUserDefaults.standardUserDefaults()
385394

386395
return defaults.objectForKey(keyName) as? Memento ?? Memento()
@@ -459,6 +468,9 @@ var testChambers = TestChambers()
459468
testChambers.observer = observerInstance
460469
testChambers.testChamberNumber++
461470
/*:
471+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
472+
*/
473+
/*:
462474
🐉 State
463475
---------
464476

@@ -518,6 +530,9 @@ context.changeStateToAuthorized(userId: "admin")
518530
context.changeStateToUnauthorized()
519531
(context.isAuthorized, context.userId)
520532
/*:
533+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
534+
*/
535+
/*:
521536
💡 Strategy
522537
-----------
523538

@@ -561,7 +576,9 @@ lower.printString("O tempora, o mores!")
561576

562577
var upper = Printer(strategy:UpperCaseStrategy())
563578
upper.printString("O tempora, o mores!")
564-
579+
/*:
580+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
581+
*/
565582
/*:
566583
🏃 Visitor
567584
----------
@@ -609,3 +626,6 @@ let names = planets.map { (planet: Planet) -> String in
609626
}
610627

611628
names
629+
/*:
630+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
631+
*/

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ enum NumberType {
6161
case NextStep, Swift
6262
}
6363

64-
class NumberHelper {
65-
class func factoryFor(type : NumberType) -> NumberFactory {
64+
enum NumberHelper {
65+
static func factoryFor(type : NumberType) -> NumberFactory {
6666
switch type {
6767
case .NextStep:
6868
return NextStepNumber.make
@@ -135,6 +135,9 @@ let empire = DeathStarBuilder { builder in
135135

136136
let deathStar = DeathStar(builder:empire)
137137
/*:
138+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
139+
*/
140+
/*:
138141
🏭 Factory Method
139142
-----------------
140143

@@ -171,8 +174,8 @@ enum Country {
171174
case UnitedStates, Spain, UK, Greece
172175
}
173176

174-
class CurrencyFactory {
175-
class func currencyForCountry(country:Country) -> Currency? {
177+
enum CurrencyFactory {
178+
static func currencyForCountry(country:Country) -> Currency? {
176179

177180
switch country {
178181
case .Spain, .Greece :
@@ -229,6 +232,9 @@ Christoph.name = "Christoph"
229232
let Eduardo = Prototype.clone()
230233
Eduardo.name = "Eduardo"
231234
/*:
235+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
236+
*/
237+
/*:
232238
💍 Singleton
233239
------------
234240

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ let oldFormat = OldDeathStarSuperlaserTarget(target)
6262
oldFormat.angleH
6363
oldFormat.angleV
6464
/*:
65+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
66+
*/
67+
/*:
6568
🌉 Bridge
6669
----------
6770

@@ -244,15 +247,15 @@ The facade pattern is used to define a simplified interface to a more complex su
244247

245248
### Example
246249
*/
247-
class Eternal {
250+
enum Eternal {
248251

249-
class func setObject(value: AnyObject!, forKey defaultName: String!) {
252+
static func setObject(value: AnyObject!, forKey defaultName: String!) {
250253
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
251254
defaults.setObject(value, forKey:defaultName)
252255
defaults.synchronize()
253256
}
254257

255-
class func objectForKey(defaultName: String!) -> AnyObject! {
258+
static func objectForKey(defaultName: String!) -> AnyObject! {
256259
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
257260

258261
return defaults.objectForKey(defaultName)

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ A short cheat-sheet with Xcode 7beta Playground ([Design-Patterns.playground.zip
1111
* [Creational](#creational)
1212
* [Structural](#structural)
1313

14+
15+
```swift
16+
Behavioral |
17+
[Creational](Creational) |
18+
[Structural](Structural)
19+
```
20+
1421
Behavioral
1522
==========
1623

@@ -117,6 +124,8 @@ atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value
117124
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
118125
```
119126

127+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
128+
120129
👫 Command
121130
----------
122131

@@ -280,6 +289,8 @@ intContext.assign(c, value: 3)
280289
var result = expression?.evaluate(intContext)
281290
```
282291

292+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
293+
283294
🍫 Iterator
284295
-----------
285296

@@ -384,6 +395,8 @@ messagesMediator.addColleague(user1)
384395
user0.send("Hello") // user1 receives message
385396
```
386397

398+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
399+
387400
💾 Memento
388401
----------
389402

@@ -519,6 +532,8 @@ testChambers.observer = observerInstance
519532
testChambers.testChamberNumber++
520533
```
521534

535+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
536+
522537
🐉 State
523538
---------
524539

@@ -584,6 +599,8 @@ context.changeStateToUnauthorized()
584599
(context.isAuthorized, context.userId)
585600
```
586601

602+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
603+
587604
💡 Strategy
588605
-----------
589606

@@ -635,6 +652,8 @@ upper.printString("O tempora, o mores!")
635652

636653
```
637654

655+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
656+
638657
🏃 Visitor
639658
----------
640659

@@ -686,6 +705,11 @@ let names = planets.map { (planet: Planet) -> String in
686705
}
687706

688707
names
708+
```
709+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
710+
711+
```swift
712+
689713
[Behavioral](Behavioral) |
690714
Creational |
691715
[Structural](Structural)
@@ -845,6 +869,9 @@ let empire = DeathStarBuilder { builder in
845869
let deathStar = DeathStar(builder:empire)
846870
```
847871

872+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
873+
874+
848875
🏭 Factory Method
849876
-----------------
850877

@@ -951,6 +978,8 @@ let Eduardo = Prototype.clone()
951978
Eduardo.name = "Eduardo"
952979
```
953980

981+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
982+
954983
💍 Singleton
955984
------------
956985

@@ -1056,6 +1085,8 @@ oldFormat.angleH
10561085
oldFormat.angleV
10571086
```
10581087

1088+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
1089+
10591090
🌉 Bridge
10601091
----------
10611092

source/behavioral/chain_of_responsibility.swift

+3
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ atm.canWithdraw(310) // Cannot because ATM has only 300
8585
atm.canWithdraw(100) // Can withdraw - 1x100
8686
atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value of 5
8787
atm.canWithdraw(30) // Can withdraw - 1x20, 2x10
88+
/*:
89+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
90+
*/

source/behavioral/interpreter.swift

+3
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@ intContext.assign(b, value: 1)
8888
intContext.assign(c, value: 3)
8989

9090
var result = expression?.evaluate(intContext)
91+
/*:
92+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
93+
*/

source/behavioral/mediator.swift

+3
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ messagesMediator.addColleague(user0)
6262
messagesMediator.addColleague(user1)
6363

6464
user0.send("Hello") // user1 receives message
65+
/*:
66+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
67+
*/

source/behavioral/memento.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class GameState {
3030
/*:
3131
Caretaker
3232
*/
33-
class CheckPoint {
34-
class func saveState(memento: Memento, keyName: String = DPMementoGameState) {
33+
enum CheckPoint {
34+
static func saveState(memento: Memento, keyName: String = DPMementoGameState) {
3535
let defaults = NSUserDefaults.standardUserDefaults()
3636
defaults.setObject(memento, forKey: keyName)
3737
defaults.synchronize()
3838
}
3939

40-
class func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
40+
static func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
4141
let defaults = NSUserDefaults.standardUserDefaults()
4242

4343
return defaults.objectForKey(keyName) as? Memento ?? Memento()

source/behavioral/observer.swift

+3
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ var observerInstance = Observer()
4646
var testChambers = TestChambers()
4747
testChambers.observer = observerInstance
4848
testChambers.testChamberNumber++
49+
/*:
50+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
51+
*/

source/behavioral/state.swift

+3
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ context.changeStateToAuthorized(userId: "admin")
5757
(context.isAuthorized, context.userId) // now logged in as "admin"
5858
context.changeStateToUnauthorized()
5959
(context.isAuthorized, context.userId)
60+
/*:
61+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
62+
*/

source/behavioral/strategy.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ lower.printString("O tempora, o mores!")
4242

4343
var upper = Printer(strategy:UpperCaseStrategy())
4444
upper.printString("O tempora, o mores!")
45-
45+
/*:
46+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
47+
*/

source/behavioral/visitor.swift

+3
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ let names = planets.map { (planet: Planet) -> String in
4545
}
4646

4747
names
48+
/*:
49+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
50+
*/

source/creational/abstract_factory.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ enum NumberType {
4848
case NextStep, Swift
4949
}
5050

51-
class NumberHelper {
52-
class func factoryFor(type : NumberType) -> NumberFactory {
51+
enum NumberHelper {
52+
static func factoryFor(type : NumberType) -> NumberFactory {
5353
switch type {
5454
case .NextStep:
5555
return NextStepNumber.make

source/creational/builder.swift

+3
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ let empire = DeathStarBuilder { builder in
5151
}
5252

5353
let deathStar = DeathStar(builder:empire)
54+
/*:
55+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
56+
*/

source/creational/factory.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ enum Country {
3535
case UnitedStates, Spain, UK, Greece
3636
}
3737

38-
class CurrencyFactory {
39-
class func currencyForCountry(country:Country) -> Currency? {
38+
enum CurrencyFactory {
39+
static func currencyForCountry(country:Country) -> Currency? {
4040

4141
switch country {
4242
case .Spain, .Greece :

source/creational/prototype.swift

+3
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ Christoph.name = "Christoph"
3232

3333
let Eduardo = Prototype.clone()
3434
Eduardo.name = "Eduardo"
35+
/*:
36+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
37+
*/

source/structural/adapter.swift

+3
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ let oldFormat = OldDeathStarSuperlaserTarget(target)
4848

4949
oldFormat.angleH
5050
oldFormat.angleV
51+
/*:
52+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
53+
*/

source/structural/facade.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ The facade pattern is used to define a simplified interface to a more complex su
66

77
### Example
88
*/
9-
class Eternal {
9+
enum Eternal {
1010

11-
class func setObject(value: AnyObject!, forKey defaultName: String!) {
11+
static func setObject(value: AnyObject!, forKey defaultName: String!) {
1212
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
1313
defaults.setObject(value, forKey:defaultName)
1414
defaults.synchronize()
1515
}
1616

17-
class func objectForKey(defaultName: String!) -> AnyObject! {
17+
static func objectForKey(defaultName: String!) -> AnyObject! {
1818
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
1919

2020
return defaults.objectForKey(defaultName)

0 commit comments

Comments
 (0)