Skip to content

Commit c7e2dfc

Browse files
author
Sebastian Boldt
committed
added visitor and facade
1 parent b6ea2e2 commit c7e2dfc

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
protocol Country {
6+
func accept(visitor: Visitor)
7+
}
8+
9+
protocol Visitor {
10+
var age: Int { get }
11+
func visit(country: Country)
12+
}
13+
14+
class Germany: Country {
15+
func accept(visitor: Visitor) {
16+
if visitor.age >= 18 {
17+
visitor.visit(country: self)
18+
}
19+
}
20+
}
21+
22+
class USA: Country {
23+
func accept(visitor: Visitor) {
24+
if visitor.age >= 18 {
25+
visitor.visit(country: self)
26+
}
27+
}
28+
}
29+
30+
class Peter: Visitor {
31+
var age: Int
32+
33+
init(age: Int) {
34+
self.age = age
35+
}
36+
var visitedCountries: [Country] = []
37+
func visit(country: Country) {
38+
visitedCountries.append(country)
39+
}
40+
}
41+
42+
let usa = USA()
43+
let germany = Germany()
44+
45+
let peter = Peter(age: 19)
46+
47+
germany.accept(visitor: peter)
48+
49+
print(peter.visitedCountries.last!)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
* ✅ Iterator
1313
* ✅ Strategy
1414
* ✅ Mediator
15+
* ✅ Visitor
1516
* ❌ Interpreter
1617
* ❌ Memento
1718
* ❌ Observer
1819
* ❌ State
19-
* ❌ Visitor
2020

2121
##Creational
2222

@@ -33,7 +33,7 @@
3333
* ✅ Decorator
3434
* ✅ Composite
3535
* ✅ Adapter
36-
* Facade
36+
* Facade
3737
* ❌ Flyweight
3838
* ❌ Protection Proxy
3939
* ❌ Virtual Proxy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
//The facade pattern is used to define a simplified interface to a more complex subsystem.
6+
7+
enum Eternal {
8+
9+
static func set(_ object: Any, forKey defaultName: String) {
10+
let defaults: UserDefaults = UserDefaults.standard
11+
defaults.set(object, forKey:defaultName)
12+
defaults.synchronize()
13+
}
14+
15+
static func object(forKey key: String) -> AnyObject! {
16+
let defaults: UserDefaults = UserDefaults.standard
17+
return defaults.object(forKey: key) as AnyObject!
18+
}
19+
20+
}
21+
22+
Eternal.set("Disconnect me. I’d rather be nothing", forKey:"Bishop")
23+
Eternal.object(forKey: "Bishop")

Structural.playground/contents.xcplayground

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<page name='Decorator'/>
66
<page name='Composite'/>
77
<page name='Adapter'/>
8+
<page name='Facade'/>
89
</pages>
910
</playground>

0 commit comments

Comments
 (0)