Skip to content

Commit df3dd8b

Browse files
codethoughtexpdorriss
authored andcommitted
new example for the Template Pattern
1 parent 6ac2afe commit df3dd8b

File tree

5 files changed

+163
-14
lines changed

5 files changed

+163
-14
lines changed

Design-Patterns.playground.zip

438 Bytes
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
@@ -615,6 +615,77 @@ upper.print("O tempora, o mores!")
615615
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Strategy)
616616
*/
617617
/*:
618+
🍪 Template
619+
-----------
620+
621+
The Template Pattern is used when two or more implementations of an
622+
algorithm exist. The template is defined and then built upon with further
623+
variations. Use this method when most (or all) subclasses need to implement
624+
the same behavior. Traditionally, this would be accomplished with abstract
625+
classes and protected methods (as in Java). However in Swift, because
626+
abstract classes don't exist (yet - maybe someday), we need to accomplish
627+
the behavior using interface delegation.
628+
629+
630+
### Example
631+
*/
632+
633+
protocol ICodeGenerator {
634+
func crossCompile()
635+
}
636+
637+
protocol IGeneratorPhases {
638+
func collectSource()
639+
func crossCompile()
640+
}
641+
642+
class CodeGenerator : ICodeGenerator{
643+
var delegate: IGeneratorPhases
644+
645+
init(delegate: IGeneratorPhases) {
646+
self.delegate = delegate
647+
}
648+
649+
650+
//Template method
651+
final func crossCompile() {
652+
delegate.collectSource()
653+
delegate.crossCompile()
654+
}
655+
}
656+
657+
class HTMLGeneratorPhases : IGeneratorPhases {
658+
func collectSource() {
659+
print("HTMLGeneratorPhases collectSource() executed")
660+
}
661+
662+
func crossCompile() {
663+
print("HTMLGeneratorPhases crossCompile() executed")
664+
}
665+
}
666+
667+
class JSONGeneratorPhases : IGeneratorPhases {
668+
func collectSource() {
669+
print("JSONGeneratorPhases collectSource() executed")
670+
}
671+
672+
func crossCompile() {
673+
print("JSONGeneratorPhases crossCompile() executed")
674+
}
675+
}
676+
677+
678+
679+
/*:
680+
### Usage
681+
*/
682+
683+
let htmlGen : ICodeGenerator = CodeGenerator(delegate: HTMLGeneratorPhases())
684+
let jsonGen: ICodeGenerator = CodeGenerator(delegate: JSONGeneratorPhases())
685+
686+
htmlGen.crossCompile()
687+
jsonGen.crossCompile()
688+
/*:
618689
🏃 Visitor
619690
----------
620691

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

Lines changed: 6 additions & 6 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

README.md

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,83 @@ upper.print("O tempora, o mores!")
715715

716716
```
717717

718+
🍪 Template
719+
-----------
720+
721+
The Template Pattern is used when two or more implementations of an
722+
algorithm exist. The template is defined and then built upon with further
723+
variations. Use this method when most (or all) subclasses need to implement
724+
the same behavior. Traditionally, this would be accomplished with abstract
725+
classes and protected methods (as in Java). However in Swift, because
726+
abstract classes don't exist (yet - maybe someday), we need to accomplish
727+
the behavior using interface delegation.
728+
729+
730+
### Example
731+
732+
```swift
733+
734+
735+
protocol ICodeGenerator {
736+
func crossCompile()
737+
}
738+
739+
protocol IGeneratorPhases {
740+
func collectSource()
741+
func crossCompile()
742+
}
743+
744+
class CodeGenerator : ICodeGenerator{
745+
var delegate: IGeneratorPhases
746+
747+
init(delegate: IGeneratorPhases) {
748+
self.delegate = delegate
749+
}
750+
751+
752+
//Template method
753+
final func crossCompile() {
754+
delegate.collectSource()
755+
delegate.crossCompile()
756+
}
757+
}
758+
759+
class HTMLGeneratorPhases : IGeneratorPhases {
760+
func collectSource() {
761+
print("HTMLGeneratorPhases collectSource() executed")
762+
}
763+
764+
func crossCompile() {
765+
print("HTMLGeneratorPhases crossCompile() executed")
766+
}
767+
}
768+
769+
class JSONGeneratorPhases : IGeneratorPhases {
770+
func collectSource() {
771+
print("JSONGeneratorPhases collectSource() executed")
772+
}
773+
774+
func crossCompile() {
775+
print("JSONGeneratorPhases crossCompile() executed")
776+
}
777+
}
778+
779+
780+
781+
```
782+
783+
### Usage
784+
785+
```swift
786+
787+
788+
let htmlGen : ICodeGenerator = CodeGenerator(delegate: HTMLGeneratorPhases())
789+
let jsonGen: ICodeGenerator = CodeGenerator(delegate: JSONGeneratorPhases())
790+
791+
htmlGen.crossCompile()
792+
jsonGen.crossCompile()
793+
```
794+
718795
🏃 Visitor
719796
----------
720797

@@ -1244,11 +1321,11 @@ protocol Shape {
12441321
func draw(fillColor: String)
12451322
}
12461323
```
1247-
1324+
12481325
Leafs
12491326

12501327
```swift
1251-
1328+
12521329
final class Square : Shape {
12531330
func draw(fillColor: String) {
12541331
print("Drawing a Square with color \(fillColor)")
@@ -1269,11 +1346,11 @@ Composite
12691346

12701347
final class Whiteboard : Shape {
12711348
lazy var shapes = [Shape]()
1272-
1349+
12731350
init(_ shapes:Shape...) {
12741351
self.shapes = shapes
12751352
}
1276-
1353+
12771354
func draw(fillColor: String) {
12781355
for shape in self.shapes {
12791356
shape.draw(fillColor: fillColor)
@@ -1287,7 +1364,7 @@ final class Whiteboard : Shape {
12871364
```swift
12881365

12891366
var whiteboard = Whiteboard(Circle(), Square())
1290-
whiteboard.draw("Red")
1367+
whiteboard.draw(fillColor: "Red")
12911368
```
12921369

12931370
🍧 Decorator
@@ -1528,7 +1605,7 @@ computer.open(doors: podBay)
15281605
🍬 Virtual Proxy
15291606
----------------
15301607

1531-
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object.
1608+
The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object.
15321609
Virtual proxy is used for loading object on demand.
15331610

15341611
### Example

source/behavioral/template.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ The Template Pattern is used when two or more implementations of an
66
algorithm exist. The template is defined and then built upon with further
77
variations. Use this method when most (or all) subclasses need to implement
88
the same behavior. Traditionally, this would be accomplished with abstract
9-
classes (as in Java). However in Swift, because abstract classes don't yet
10-
exist, we need to accomplish the behavior using interface delegation.
9+
classes and protected methods (as in Java). However in Swift, because
10+
abstract classes don't exist (yet - maybe someday), we need to accomplish
11+
the behavior using interface delegation.
1112

1213

1314
### Example

0 commit comments

Comments
 (0)