Skip to content

Commit d7ec747

Browse files
author
Sebastian Boldt
committed
Decorator Pattern
1 parent 858f655 commit d7ec747

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Inspired by: https://github.com/ochococo/Design-Patterns-In-Swift
3131
##Structural
3232

3333
* ✅ Bridge
34+
* ✅ Decorator
3435
* ❌ Adapter
3536
* ❌ Composite
36-
* ❌ Decorator
3737
* ❌ Facade
3838
* ❌ Flyweight
3939
* ❌ Protection Proxy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
protocol Pizza {
6+
func getIngredients() -> [String]
7+
func printIngredients()
8+
}
9+
10+
extension Pizza {
11+
func printIngredients() {
12+
for (index,value) in self.getIngredients().enumerated() {
13+
print("Ingredient(\(index+1)):\(value)")
14+
}
15+
}
16+
}
17+
18+
class BasicPizza : Pizza {
19+
func getIngredients() -> [String] {
20+
return ["Dough","Tomato-Sauce","Cheese"]
21+
}
22+
}
23+
24+
final class HawaiDecorator : Pizza {
25+
26+
private var decoratedPizza : Pizza
27+
28+
required init (decoratedPizza: Pizza) {
29+
self.decoratedPizza = decoratedPizza
30+
}
31+
32+
func getIngredients() -> [String] {
33+
return self.decoratedPizza.getIngredients() + ["Boild Ham","Pineapple"]
34+
}
35+
}
36+
37+
final class CheeseWheelDecorator : Pizza {
38+
private var decoratedPizza : Pizza
39+
40+
required init (decoratedPizza: Pizza) {
41+
self.decoratedPizza = decoratedPizza
42+
}
43+
44+
func getIngredients() -> [String] {
45+
return self.decoratedPizza.getIngredients() + ["More Cheese for the surrounding"]
46+
}
47+
}
48+
49+
50+
let basicPizza = BasicPizza()
51+
let hawaiPizza = HawaiDecorator(decoratedPizza: basicPizza)
52+
let hawaiWithCheeseWheel = CheeseWheelDecorator(decoratedPizza: hawaiPizza)
53+
54+
hawaiWithCheeseWheel.printIngredients()
55+
56+
57+
58+
59+
60+
//: [Next](@next)
+6-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='6.0' target-platform='ios'/>
2+
<playground version='6.0' target-platform='ios'>
3+
<pages>
4+
<page name='Bridge'/>
5+
<page name='Decorator'/>
6+
</pages>
7+
</playground>

0 commit comments

Comments
 (0)