Skip to content

Commit 2cb7dd5

Browse files
committed
Add further example. Include recent changes to README file into the source
1 parent 6b86c20 commit 2cb7dd5

File tree

19 files changed

+102
-22
lines changed

19 files changed

+102
-22
lines changed

Design-Patterns.playground.zip

212 Bytes
Binary file not shown.

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ 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+
*//*:
102104
👫 Command
103105
----------
104106

@@ -250,6 +252,8 @@ intContext.assign(c, value: 3)
250252

251253
var result = expression?.evaluate(intContext)
252254
/*:
255+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
256+
*//*:
253257
🍫 Iterator
254258
-----------
255259

@@ -342,6 +346,8 @@ messagesMediator.addColleague(user1)
342346

343347
user0.send("Hello") // user1 receives message
344348
/*:
349+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
350+
*//*:
345351
💾 Memento
346352
----------
347353

@@ -373,14 +379,14 @@ class GameState {
373379
/*:
374380
Caretaker
375381
*/
376-
class CheckPoint {
377-
class func saveState(memento: Memento, keyName: String = DPMementoGameState) {
382+
enum CheckPoint {
383+
static func saveState(memento: Memento, keyName: String = DPMementoGameState) {
378384
let defaults = NSUserDefaults.standardUserDefaults()
379385
defaults.setObject(memento, forKey: keyName)
380386
defaults.synchronize()
381387
}
382388

383-
class func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
389+
static func restorePreviousState(keyName keyName: String = DPMementoGameState) -> Memento {
384390
let defaults = NSUserDefaults.standardUserDefaults()
385391

386392
return defaults.objectForKey(keyName) as? Memento ?? Memento()
@@ -459,6 +465,8 @@ var testChambers = TestChambers()
459465
testChambers.observer = observerInstance
460466
testChambers.testChamberNumber++
461467
/*:
468+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
469+
*//*:
462470
🐉 State
463471
---------
464472

@@ -518,6 +526,8 @@ context.changeStateToAuthorized(userId: "admin")
518526
context.changeStateToUnauthorized()
519527
(context.isAuthorized, context.userId)
520528
/*:
529+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
530+
*//*:
521531
💡 Strategy
522532
-----------
523533

@@ -561,8 +571,9 @@ lower.printString("O tempora, o mores!")
561571

562572
var upper = Printer(strategy:UpperCaseStrategy())
563573
upper.printString("O tempora, o mores!")
564-
565574
/*:
575+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
576+
*//*:
566577
🏃 Visitor
567578
----------
568579

@@ -609,3 +620,6 @@ let names = planets.map { (planet: Planet) -> String in
609620
}
610621

611622
names
623+
/*:
624+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Visitor)
625+
*/

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

+8-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
@@ -134,6 +134,8 @@ let empire = DeathStarBuilder { builder in
134134
}
135135

136136
let deathStar = DeathStar(builder:empire)
137+
138+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
137139
/*:
138140
🏭 Factory Method
139141
-----------------
@@ -171,8 +173,8 @@ enum Country {
171173
case UnitedStates, Spain, UK, Greece
172174
}
173175

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

177179
switch country {
178180
case .Spain, .Greece :
@@ -229,6 +231,8 @@ Christoph.name = "Christoph"
229231
let Eduardo = Prototype.clone()
230232
Eduardo.name = "Eduardo"
231233
/*:
234+
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
235+
*//*:
232236
💍 Singleton
233237
------------
234238

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ 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+
*//*:
6567
🌉 Bridge
6668
----------
6769

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

245247
### Example
246248
*/
247-
class Eternal {
249+
enum Eternal {
248250

249-
class func setObject(value: AnyObject!, forKey defaultName: String!) {
251+
static func setObject(value: AnyObject!, forKey defaultName: String!) {
250252
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
251253
defaults.setObject(value, forKey:defaultName)
252254
defaults.synchronize()
253255
}
254256

255-
class func objectForKey(defaultName: String!) -> AnyObject! {
257+
static func objectForKey(defaultName: String!) -> AnyObject! {
256258
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
257259

258260
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)