Skip to content

Commit 7b2171e

Browse files
author
Sebastian Boldt
committed
Addec Example for Factory Method Pattern
1 parent cf4c78a commit 7b2171e

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
protocol PizzaCreator {
6+
func getIngredients() -> [String]
7+
}
8+
9+
struct HawaiCreator: PizzaCreator {
10+
func getIngredients() -> [String] {
11+
return ["Ham","Pineapple","Cheese","Tomato Sauce"]
12+
}
13+
}
14+
15+
struct SalamiCreator: PizzaCreator {
16+
func getIngredients() -> [String] {
17+
return ["Salami","Cheese","Tomato Sauce"]
18+
}
19+
}
20+
21+
enum PizzaType {
22+
case Salami
23+
case Hawai
24+
}
25+
26+
func getPizzaIngredientsList(forType type: PizzaType) -> PizzaCreator {
27+
switch type {
28+
case .Hawai:
29+
return HawaiCreator()
30+
case .Salami:
31+
return SalamiCreator()
32+
}
33+
}
34+
35+
func printIngredients(creator: PizzaCreator) {
36+
print(creator.getIngredients())
37+
}
38+
39+
printIngredients(creator: getPizzaIngredientsList(forType: .Hawai))
40+
41+
//: [Next](@next)

β€ŽCreational.playground/contents.xcplayground

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<pages>
44
<page name='Singleton'/>
55
<page name='Prototype'/>
6+
<page name='Factory Method'/>
67
</pages>
78
</playground>

β€ŽREADME.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
* βœ… Singleton
2424
* βœ… Prototype
25+
* βœ… Factory Method
2526
* ❌ Abstract Factory
2627
* ❌ Builder
27-
* ❌ Factory Method
28+
2829

2930
##Structural
3031

0 commit comments

Comments
Β (0)