Skip to content

Commit 2a9831b

Browse files
author
Sebastian Boldt
committed
protection proxy and virtual proxy
1 parent f1eecf1 commit 2a9831b

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed
Binary file not shown.

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* ✅ Strategy
1414
* ✅ Mediator
1515
* ✅ Visitor
16-
* ❌ Interpreter
1716
* ❌ Memento
17+
* ❌ Interpreter
1818
* ❌ Observer
1919
* ❌ State
2020

@@ -35,5 +35,5 @@
3535
* ✅ Adapter
3636
* ✅ Flyweight
3737
* ✅ Facade
38-
* Protection Proxy
39-
* Virtual Proxy
38+
* Protection Proxy
39+
* Virtual Proxy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
enum AuthError: Error {
6+
case unauthorized
7+
}
8+
9+
class PasswordStore {
10+
func showSecretInformation() -> String {
11+
return "SecretPassword"
12+
}
13+
}
14+
15+
class PasswordTresor {
16+
var resourceToProtect: PasswordStore?
17+
func authenticate(password: String) -> Bool {
18+
guard password == "password" else {
19+
return false
20+
}
21+
resourceToProtect = PasswordStore()
22+
return true
23+
}
24+
25+
func showSecretInformation() throws -> String {
26+
guard let resourceToProtect = resourceToProtect else {
27+
throw AuthError.unauthorized
28+
}
29+
return resourceToProtect.showSecretInformation()
30+
}
31+
}
32+
33+
//Usage
34+
35+
let tresor = PasswordTresor()
36+
tresor.authenticate(password: "password")
37+
try tresor.showSecretInformation()
38+
39+
//: [Next](@next)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
protocol Server {
6+
func compute() -> String
7+
}
8+
9+
struct ExpensiveServer: Server {
10+
func compute() -> String {
11+
return "Computation completed"
12+
}
13+
}
14+
15+
class ExpensiveServerInterface: Server {
16+
lazy private var expensiveServer = ExpensiveServer()
17+
18+
func compute() -> String {
19+
return expensiveServer.compute()
20+
}
21+
}
22+
23+
let serverInterface = ExpensiveServerInterface()
24+
serverInterface.compute()
25+
26+
//: [Next](@next)

Structural.playground/contents.xcplayground

+2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
<page name='Adapter'/>
88
<page name='Flyweight'/>
99
<page name='Facade'/>
10+
<page name='Protection Proxy'/>
11+
<page name='Virtual Proxy'/>
1012
</pages>
1113
</playground>

0 commit comments

Comments
 (0)