Skip to content

Commit f1eecf1

Browse files
author
Sebastian Boldt
committed
added abstract factory example
1 parent d869917 commit f1eecf1

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
enum Skill {
6+
case Junior
7+
case Senior
8+
}
9+
10+
protocol Developer {
11+
var skill: Skill { get }
12+
func code() -> String
13+
static func make(skill: Skill) -> Developer
14+
}
15+
16+
typealias DeveloperFactory = (Skill) -> Developer
17+
18+
struct FrontEndDeveloper: Developer {
19+
var skill: Skill
20+
21+
func code() -> String {
22+
return "<body> Hello World </body>"
23+
}
24+
25+
static func make(skill: Skill) -> Developer {
26+
return FrontEndDeveloper(skill: skill)
27+
}
28+
}
29+
30+
struct BackendDeveloper: Developer {
31+
var skill: Skill
32+
33+
func code() -> String {
34+
return "SELECT * FROM DEVELOPERS"
35+
}
36+
37+
static func make(skill: Skill) -> Developer {
38+
return BackendDeveloper(skill: skill)
39+
}
40+
}
41+
42+
// Abstract Factory
43+
44+
enum DeveloperType {
45+
case backend
46+
case frontend
47+
}
48+
49+
struct AbstractDeveloperFactory {
50+
static func factory(for type: DeveloperType) -> DeveloperFactory {
51+
switch type {
52+
case .backend:
53+
return BackendDeveloper.make
54+
case .frontend:
55+
return FrontEndDeveloper.make
56+
}
57+
}
58+
}
59+
60+
61+
// Usage
62+
63+
let frontEndFactory = AbstractDeveloperFactory.factory(for: .frontend)
64+
let frontEndDev = frontEndFactory(.Junior)
65+
frontEndDev.code()
66+
67+
//: [Next](@next)

β€ŽCreational.playground/contents.xcplayground

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<page name='Prototype'/>
66
<page name='Factory Method'/>
77
<page name='Builder'/>
8+
<page name='Abstract Factory'/>
89
</pages>
910
</playground>

β€ŽGOFSwift.xcworkspace/contents.xcworkspacedata

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽREADME.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* βœ… Prototype
2525
* βœ… Factory Method
2626
* βœ… Builder
27-
* ❌ Abstract Factory
27+
* βœ… Abstract Factory
2828

2929

3030
## Structural

0 commit comments

Comments
Β (0)