Skip to content

Commit fad0d2b

Browse files
committed
new example for the Template Pattern
1 parent 114dc8a commit fad0d2b

File tree

5 files changed

+212
-16
lines changed

5 files changed

+212
-16
lines changed

Design-Patterns.playground.zip

6.62 KB
Binary file not shown.

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,77 @@ upper.print("O tempora, o mores!")
606606
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
607607
*/
608608
/*:
609+
🍪 Template
610+
-----------
611+
612+
The Template Pattern is used when two or more implementations of an
613+
algorithm exist. The template is defined and then built upon with further
614+
variations. Use this method when most (or all) subclasses need to implement
615+
the same behavior. Traditionally, this would be accomplished with abstract
616+
classes and protected methods (as in Java). However in Swift, because
617+
abstract classes don't exist (yet - maybe someday), we need to accomplish
618+
the behavior using interface delegation.
619+
620+
621+
### Example
622+
*/
623+
624+
protocol ICodeGenerator {
625+
func crossCompile()
626+
}
627+
628+
protocol IGeneratorPhases {
629+
func collectSource()
630+
func crossCompile()
631+
}
632+
633+
class CodeGenerator : ICodeGenerator{
634+
var delegate: IGeneratorPhases
635+
636+
init(delegate: IGeneratorPhases) {
637+
self.delegate = delegate
638+
}
639+
640+
641+
//Template method
642+
final func crossCompile() {
643+
delegate.collectSource()
644+
delegate.crossCompile()
645+
}
646+
}
647+
648+
class HTMLGeneratorPhases : IGeneratorPhases {
649+
func collectSource() {
650+
print("HTMLGeneratorPhases collectSource() executed")
651+
}
652+
653+
func crossCompile() {
654+
print("HTMLGeneratorPhases crossCompile() executed")
655+
}
656+
}
657+
658+
class JSONGeneratorPhases : IGeneratorPhases {
659+
func collectSource() {
660+
print("JSONGeneratorPhases collectSource() executed")
661+
}
662+
663+
func crossCompile() {
664+
print("JSONGeneratorPhases crossCompile() executed")
665+
}
666+
}
667+
668+
669+
670+
/*:
671+
### Usage
672+
*/
673+
674+
let htmlGen : ICodeGenerator = CodeGenerator(delegate: HTMLGeneratorPhases())
675+
let jsonGen: ICodeGenerator = CodeGenerator(delegate: JSONGeneratorPhases())
676+
677+
htmlGen.crossCompile()
678+
jsonGen.crossCompile()
679+
/*:
609680
🏃 Visitor
610681
----------
611682

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ Component
126126
protocol Shape {
127127
func draw(fillColor: String)
128128
}
129-
/*:
129+
/*:
130130
Leafs
131-
*/
131+
*/
132132
final class Square : Shape {
133133
func draw(fillColor: String) {
134134
print("Drawing a Square with color \(fillColor)")
@@ -146,11 +146,11 @@ Composite
146146
*/
147147
final class Whiteboard : Shape {
148148
lazy var shapes = [Shape]()
149-
149+
150150
init(_ shapes:Shape...) {
151151
self.shapes = shapes
152152
}
153-
153+
154154
func draw(fillColor: String) {
155155
for shape in self.shapes {
156156
shape.draw(fillColor: fillColor)
@@ -161,7 +161,7 @@ final class Whiteboard : Shape {
161161
### Usage:
162162
*/
163163
var whiteboard = Whiteboard(Circle(), Square())
164-
whiteboard.draw("Red")
164+
whiteboard.draw(fillColor: "Red")
165165
/*:
166166
🍧 Decorator
167167
------------
@@ -377,7 +377,7 @@ computer.open(doors: podBay)
377377
🍬 Virtual Proxy
378378
----------------
379379

380-
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object.
380+
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object.
381381
Virtual proxy is used for loading object on demand.
382382

383383
### Example
@@ -388,7 +388,7 @@ protocol HEVSuitMedicalAid {
388388

389389
class HEVSuit : HEVSuitMedicalAid {
390390
func administerMorphine() -> String {
391-
return "Morphine aministered."
391+
return "Morphine administered."
392392
}
393393
}
394394

README.md

Lines changed: 131 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ A short cheat-sheet with Xcode 8.2 Playground ([Design-Patterns.playground.zip](
1313
* [Creational](#creational)
1414
* [Structural](#structural)
1515

16+
17+
```swift
18+
19+
Behavioral |
20+
[Creational](Creational) |
21+
[Structural](Structural)
22+
```
23+
1624
Behavioral
1725
==========
1826

@@ -126,6 +134,9 @@ atm.canWithdraw(amount: 30) // Can withdraw - 1x20, 2x10
126134

127135
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Chain-Of-Responsibility)
128136
137+
```swift
138+
139+
```
129140

130141
👫 Command
131142
----------
@@ -291,6 +302,10 @@ var result = expression.evaluate(context)
291302

292303
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Interpreter)
293304
305+
```swift
306+
307+
```
308+
294309
🍫 Iterator
295310
-----------
296311

@@ -403,6 +418,9 @@ spamMonster(message: "I'd Like to Add you to My Professional Network", worker: m
403418

404419
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Mediator)
405420
421+
```swift
422+
423+
```
406424

407425
💾 Memento
408426
----------
@@ -557,6 +575,9 @@ testChambers.testChamberNumber += 1
557575

558576
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Observer)
559577
578+
```swift
579+
580+
```
560581

561582
🐉 State
562583
---------
@@ -625,6 +646,9 @@ userContext.changeStateToUnauthorized()
625646

626647
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-State)
627648
649+
```swift
650+
651+
```
628652

629653
💡 Strategy
630654
-----------
@@ -678,6 +702,87 @@ upper.print("O tempora, o mores!")
678702

679703
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
680704
705+
```swift
706+
707+
```
708+
709+
🍪 Template
710+
-----------
711+
712+
The Template Pattern is used when two or more implementations of an
713+
algorithm exist. The template is defined and then built upon with further
714+
variations. Use this method when most (or all) subclasses need to implement
715+
the same behavior. Traditionally, this would be accomplished with abstract
716+
classes and protected methods (as in Java). However in Swift, because
717+
abstract classes don't exist (yet - maybe someday), we need to accomplish
718+
the behavior using interface delegation.
719+
720+
721+
### Example
722+
723+
```swift
724+
725+
726+
protocol ICodeGenerator {
727+
func crossCompile()
728+
}
729+
730+
protocol IGeneratorPhases {
731+
func collectSource()
732+
func crossCompile()
733+
}
734+
735+
class CodeGenerator : ICodeGenerator{
736+
var delegate: IGeneratorPhases
737+
738+
init(delegate: IGeneratorPhases) {
739+
self.delegate = delegate
740+
}
741+
742+
743+
//Template method
744+
final func crossCompile() {
745+
delegate.collectSource()
746+
delegate.crossCompile()
747+
}
748+
}
749+
750+
class HTMLGeneratorPhases : IGeneratorPhases {
751+
func collectSource() {
752+
print("HTMLGeneratorPhases collectSource() executed")
753+
}
754+
755+
func crossCompile() {
756+
print("HTMLGeneratorPhases crossCompile() executed")
757+
}
758+
}
759+
760+
class JSONGeneratorPhases : IGeneratorPhases {
761+
func collectSource() {
762+
print("JSONGeneratorPhases collectSource() executed")
763+
}
764+
765+
func crossCompile() {
766+
print("JSONGeneratorPhases crossCompile() executed")
767+
}
768+
}
769+
770+
771+
772+
```
773+
774+
### Usage
775+
776+
```swift
777+
778+
779+
let htmlGen : ICodeGenerator = CodeGenerator(delegate: HTMLGeneratorPhases())
780+
let jsonGen: ICodeGenerator = CodeGenerator(delegate: JSONGeneratorPhases())
781+
782+
htmlGen.crossCompile()
783+
jsonGen.crossCompile()
784+
```
785+
681786
🏃 Visitor
682787
----------
683788

@@ -771,6 +876,10 @@ The abstract factory pattern is used to provide a client with a set of related o
771876
The "family" of objects created by the factory are determined at run-time.
772877

773878
### Example
879+
880+
```swift
881+
882+
```
774883

775884
Protocols
776885

@@ -903,6 +1012,10 @@ let deathStar = DeathStar(builder:empire)
9031012

9041013
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Builder)
9051014
1015+
```swift
1016+
1017+
```
1018+
9061019
🏭 Factory Method
9071020
-----------------
9081021

@@ -1011,6 +1124,10 @@ Eduardo.name = "Eduardo"
10111124

10121125
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
10131126
1127+
```swift
1128+
1129+
```
1130+
10141131
💍 Singleton
10151132
------------
10161133

@@ -1118,6 +1235,10 @@ oldFormat.angleV
11181235

11191236
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Adapter)
11201237
1238+
```swift
1239+
1240+
```
1241+
11211242
🌉 Bridge
11221243
----------
11231244

@@ -1191,11 +1312,11 @@ protocol Shape {
11911312
func draw(fillColor: String)
11921313
}
11931314
```
1194-
1315+
11951316
Leafs
11961317

11971318
```swift
1198-
1319+
11991320
final class Square : Shape {
12001321
func draw(fillColor: String) {
12011322
print("Drawing a Square with color \(fillColor)")
@@ -1216,11 +1337,11 @@ Composite
12161337

12171338
final class Whiteboard : Shape {
12181339
lazy var shapes = [Shape]()
1219-
1340+
12201341
init(_ shapes:Shape...) {
12211342
self.shapes = shapes
12221343
}
1223-
1344+
12241345
func draw(fillColor: String) {
12251346
for shape in self.shapes {
12261347
shape.draw(fillColor: fillColor)
@@ -1234,7 +1355,7 @@ final class Whiteboard : Shape {
12341355
```swift
12351356

12361357
var whiteboard = Whiteboard(Circle(), Square())
1237-
whiteboard.draw("Red")
1358+
whiteboard.draw(fillColor: "Red")
12381359
```
12391360

12401361
🍧 Decorator
@@ -1475,7 +1596,7 @@ computer.open(doors: podBay)
14751596
🍬 Virtual Proxy
14761597
----------------
14771598

1478-
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object.
1599+
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object.
14791600
Virtual proxy is used for loading object on demand.
14801601

14811602
### Example
@@ -1488,7 +1609,7 @@ protocol HEVSuitMedicalAid {
14881609

14891610
class HEVSuit : HEVSuitMedicalAid {
14901611
func administerMorphine() -> String {
1491-
return "Morphine aministered."
1612+
return "Morphine administered."
14921613
}
14931614
}
14941615

@@ -1514,3 +1635,6 @@ Info
15141635
====
15151636

15161637
📖 Descriptions from: [Gang of Four Design Patterns Reference Sheet](http://www.blackwasp.co.uk/GangOfFour.aspx)
1638+
1639+
1640+
```swift

0 commit comments

Comments
 (0)