Skip to content

Commit 07e05d4

Browse files
author
Oktawian Chojnacki
committed
Protection Proxy + Virtual Proxy
1 parent 258eb79 commit 07e05d4

27 files changed

+358
-205
lines changed

Design-Patterns.playground.zip

3.14 KB
Binary file not shown.

Design-Patterns.playground/Documentation/section-43.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="-proxy">🚧 Proxy</h2>
15-
<h1 id="behavioral">Behavioral</h1>
16-
<blockquote>
17-
<p>In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.</p>
18-
<p><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Behavioral_pattern">wikipedia.org</a></p>
19-
</blockquote>
20-
<h2 id="-chain-of-responsibility">🚧 Chain Of Responsibility</h2>
21-
<h2 id="-command">👫 Command</h2>
14+
<h2 id="-virtual-proxy">🍬 Virtual Proxy</h2>
15+
<p><strong>Source:</strong></p>
2216

2317
</section>
2418
</div>

Design-Patterns.playground/Documentation/section-47.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="-iterator">🚧 Iterator</h2>
15-
<h2 id="-mediator">🚧 Mediator</h2>
16-
<h2 id="-memento">🚧 Memento</h2>
17-
<h2 id="-observer">👓 Observer</h2>
14+
<h2 id="-protection-proxy">☔ Protection Proxy</h2>
15+
<p><strong>Source:</strong></p>
1816

1917
</section>
2018
</div>

Design-Patterns.playground/Documentation/section-51.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="-state">🐉 State</h2>
14+
<h1 id="behavioral">Behavioral</h1>
15+
<blockquote>
16+
<p>In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.</p>
17+
<p><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Behavioral_pattern">wikipedia.org</a></p>
18+
</blockquote>
19+
<h2 id="-chain-of-responsibility">🚧 Chain Of Responsibility</h2>
20+
<h2 id="-command">👫 Command</h2>
1521

1622
</section>
1723
</div>

Design-Patterns.playground/Documentation/section-55.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="-strategy">💡 Strategy</h2>
14+
<h2 id="-iterator">🚧 Iterator</h2>
15+
<h2 id="-mediator">🚧 Mediator</h2>
16+
<h2 id="-memento">🚧 Memento</h2>
17+
<h2 id="-observer">👓 Observer</h2>
1518

1619
</section>
1720
</div>

Design-Patterns.playground/Documentation/section-59.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h2 id="-visitor">🏃 Visitor</h2>
14+
<h2 id="-state">🐉 State</h2>
1515

1616
</section>
1717
</div>

Design-Patterns.playground/Documentation/section-63.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
<body>
1212
<div class="content-wrapper">
1313
<section class="section">
14-
<h1 id="info">Info</h1>
15-
<p>🍺 Playground generated with: <a href="https://github.com/jas/swift-playground-builder">Swift Playground Builder</a> by <a href="http://twitter.com/jasonsandmeyer">@jasonsandmeyer</a></p>
16-
<p>🚀 How to generate playground (+zip) from this README: <a href="https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.markdown">GENERATE.markdown</a></p>
14+
<h2 id="-strategy">💡 Strategy</h2>
1715

1816
</section>
1917
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 66</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<p><strong>Usage:</strong></p>
15+
16+
</section>
17+
</div>
18+
</body>
19+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 68</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<h2 id="-visitor">🏃 Visitor</h2>
15+
16+
</section>
17+
</div>
18+
</body>
19+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 70</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<p><strong>Usage:</strong></p>
15+
16+
</section>
17+
</div>
18+
</body>
19+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Section 72</title>
6+
<meta id="xcode-display" name="xcode-display" content="render">
7+
<meta name="apple-mobile-web-app-capable" content="yes">
8+
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
9+
<link rel="stylesheet" type="text/css" href="stylesheet.css">
10+
</head>
11+
<body>
12+
<div class="content-wrapper">
13+
<section class="section">
14+
<h1 id="info">Info</h1>
15+
<p>🍺 Playground generated with: <a href="https://github.com/jas/swift-playground-builder">Swift Playground Builder</a> by <a href="http://twitter.com/jasonsandmeyer">@jasonsandmeyer</a></p>
16+
<p>🚀 How to generate playground (+zip) from this README: <a href="https://github.com/ochococo/Design-Patterns-In-Swift/blob/master/GENERATE.markdown">GENERATE.markdown</a></p>
17+
18+
</section>
19+
</div>
20+
</body>
21+
</html>

Design-Patterns.playground/contents.xcplayground

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,21 @@
129129
</code>
130130
<documentation relative-path="section-63.html">
131131
</documentation>
132+
<code source-file-name="section-64.swift">
133+
</code>
134+
<documentation relative-path="section-65.html">
135+
</documentation>
136+
<code source-file-name="section-66.swift">
137+
</code>
138+
<documentation relative-path="section-67.html">
139+
</documentation>
140+
<code source-file-name="section-68.swift">
141+
</code>
142+
<documentation relative-path="section-69.html">
143+
</documentation>
144+
<code source-file-name="section-70.swift">
145+
</code>
146+
<documentation relative-path="section-71.html">
147+
</documentation>
132148
</sections>
133149
</playground>
Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,18 @@
1-
protocol FileOperationCommand {
2-
init(file: String)
3-
func execute()
1+
protocol HEVSuitMedicalAid {
2+
func administerMorphine() -> String
43
}
54

6-
class FileMoveCommand : FileOperationCommand {
7-
let file:String
8-
required init(file: String) {
9-
self.file = file
10-
}
11-
12-
func execute() {
13-
print("\(file) moved")
5+
class HEVSuit : HEVSuitMedicalAid {
6+
func administerMorphine() -> String {
7+
return "Morphine aministered."
148
}
159
}
1610

17-
class FileDeleteCommand : FileOperationCommand {
18-
let file:String
19-
required init(file: String) {
20-
self.file = file
21-
}
11+
class HEVSuitHumanInterface : HEVSuitMedicalAid {
2212

23-
func execute() {
24-
print("\(file) deleted")
25-
}
26-
}
13+
lazy private var physicalSuit: HEVSuit = HEVSuit()
2714

28-
class FileManager {
29-
let deleteCommand: FileOperationCommand
30-
let moveCommand: FileOperationCommand
31-
32-
init(deleteCommand: FileDeleteCommand, moveCommand: FileMoveCommand) {
33-
self.deleteCommand = deleteCommand
34-
self.moveCommand = moveCommand
35-
}
36-
37-
func delete () {
38-
deleteCommand.execute()
39-
}
40-
41-
func move () {
42-
moveCommand.execute()
15+
func administerMorphine() -> String {
16+
return physicalSuit.administerMorphine()
4317
}
4418
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
let deleteCommand = FileDeleteCommand(file: "/path/to/testfile")
2-
let moveCommand = FileMoveCommand(file: "/path/to/testfile")
3-
let fileManager = FileManager(deleteCommand:deleteCommand , moveCommand: moveCommand)
4-
5-
fileManager.delete()
6-
fileManager.move()
1+
let humanInterface = HEVSuitHumanInterface()
2+
humanInterface.administerMorphine()
Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1-
class StepCounter {
2-
var totalSteps: Int = 0 {
3-
willSet(newTotalSteps) {
4-
println("About to set totalSteps to \(newTotalSteps)")
1+
protocol DoorOperator {
2+
func openDoors(doors: String) -> String
3+
}
4+
5+
class HAL9000 : DoorOperator {
6+
func openDoors(doors: String) -> String {
7+
return ("HAL9000: Affirmative, Dave. I read you. Opened \(doors).")
8+
}
9+
}
10+
11+
class CurrentComputer : DoorOperator {
12+
13+
private var computer: HAL9000!
14+
15+
func authenticateWithPassword(pass: String) -> Bool {
16+
17+
if pass != "pass" {
18+
return false
519
}
6-
didSet {
7-
if totalSteps > oldValue {
8-
println("Added \(totalSteps - oldValue) steps")
9-
}
20+
computer = HAL9000()
21+
22+
return true
23+
}
24+
25+
func openDoors(doors: String) -> String {
26+
27+
if (computer == nil) {
28+
return "Access Denied. I'm afraid I can't do that."
1029
}
30+
31+
return computer.openDoors(doors)
1132
}
1233
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
let stepCounter = StepCounter()
2-
stepCounter.totalSteps = 200
3-
// About to set totalSteps to 200
4-
// Added 200 steps
5-
stepCounter.totalSteps = 360
6-
// About to set totalSteps to 360
7-
// Added 160 steps
8-
stepCounter.totalSteps = 896
9-
// About to set totalSteps to 896
10-
// Added 536 steps
1+
let computer = CurrentComputer()
2+
let doors = "Pod Bay Doors"
3+
4+
computer.openDoors(doors)
5+
6+
computer.authenticateWithPassword("pass")
7+
computer.openDoors(doors)
Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1-
class Context {
2-
private var state: State = UnauthorizedState()
3-
func changeStateToAuthorized(#userId: String) {
4-
state = AuthorizedState(userId: userId)
5-
}
6-
func changeStateToUnauthorized() {
7-
state = UnauthorizedState()
8-
}
9-
var isAuthorized: Bool {
10-
get { return state.isAuthorized(self) }
11-
}
12-
var userId: String? {
13-
get { return state.userId(self) }
14-
}
1+
protocol FileOperationCommand {
2+
init(file: String)
3+
func execute()
154
}
165

17-
protocol State {
18-
func isAuthorized(context: Context) -> Bool
19-
func userId(context: Context) -> String?
6+
class FileMoveCommand : FileOperationCommand {
7+
let file:String
8+
required init(file: String) {
9+
self.file = file
10+
}
11+
12+
func execute() {
13+
print("\(file) moved")
14+
}
2015
}
2116

22-
class UnauthorizedState: State {
23-
func isAuthorized(context: Context) -> Bool { return false }
24-
func userId(context: Context) -> String? { return nil }
17+
class FileDeleteCommand : FileOperationCommand {
18+
let file:String
19+
required init(file: String) {
20+
self.file = file
21+
}
22+
23+
func execute() {
24+
print("\(file) deleted")
25+
}
2526
}
2627

27-
class AuthorizedState: State {
28-
let userId: String
29-
init(userId: String) { self.userId = userId }
30-
func isAuthorized(context: Context) -> Bool { return true }
31-
func userId(context: Context) -> String? { return userId }
28+
class FileManager {
29+
let deleteCommand: FileOperationCommand
30+
let moveCommand: FileOperationCommand
31+
32+
init(deleteCommand: FileDeleteCommand, moveCommand: FileMoveCommand) {
33+
self.deleteCommand = deleteCommand
34+
self.moveCommand = moveCommand
35+
}
36+
37+
func delete () {
38+
deleteCommand.execute()
39+
}
40+
41+
func move () {
42+
moveCommand.execute()
43+
}
3244
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
let context = Context()
2-
(context.isAuthorized, context.userId)
3-
context.changeStateToAuthorized(userId: "admin")
4-
(context.isAuthorized, context.userId) // now logged in as "admin"
5-
context.changeStateToUnauthorized()
6-
(context.isAuthorized, context.userId)
1+
let deleteCommand = FileDeleteCommand(file: "/path/to/testfile")
2+
let moveCommand = FileMoveCommand(file: "/path/to/testfile")
3+
let fileManager = FileManager(deleteCommand:deleteCommand , moveCommand: moveCommand)
4+
5+
fileManager.delete()
6+
fileManager.move()

0 commit comments

Comments
 (0)