Skip to content

Commit 51c8295

Browse files
committed
Merge pull request ochococo#9 from oarrabi/master
- Added Command pattern
2 parents 436a09b + c108e7e commit 51c8295

16 files changed

+247
-94
lines changed

Design-Patterns.playground.zip

-8.46 KB
Binary file not shown.

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ <h1 id="behavioral">Behavioral</h1>
2121
</blockquote>
2222
<h2 id="chain-of-responsibility">Chain Of Responsibility</h2>
2323
<h2 id="command">Command</h2>
24-
<h2 id="iterator">Iterator</h2>
25-
<h2 id="mediator">Mediator</h2>
26-
<h2 id="memento">Memento</h2>
27-
<h2 id="observer">Observer</h2>
28-
<h2 id="state">State</h2>
2924

3025
</section>
3126
</div>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
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>
18+
<h2 id="state">State</h2>
1519

1620
</section>
1721
</div>

Design-Patterns.playground/Documentation/section-43.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="strategy">Strategy</h2>
1515

1616
</section>
1717
</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 48</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 50</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>

Design-Patterns.playground/contents.xcplayground

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,13 @@
9595
</documentation>
9696
<code source-file-name="section-46.swift">
9797
</code>
98+
<documentation relative-path="section-47.html">
99+
</documentation>
100+
<code source-file-name="section-48.swift">
101+
</code>
102+
<documentation relative-path="section-49.html">
103+
</documentation>
104+
<code source-file-name="section-50.swift">
105+
</code>
98106
</sections>
99107
</playground>
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()
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
protocol PrintStrategy {
2-
func printString(string: String) -> String
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+
}
315
}
416

5-
class Printer {
6-
7-
let strategy: PrintStrategy
8-
9-
func printString(string:String)->String{
10-
return self.strategy.printString(string);
11-
}
12-
13-
init(strategy: PrintStrategy){
14-
self.strategy = strategy
15-
}
17+
protocol State {
18+
func isAuthorized(context: Context) -> Bool
19+
func userId(context: Context) -> String?
1620
}
1721

18-
class UpperCaseStrategy: PrintStrategy{
19-
func printString(string:String)->String{
20-
return string.uppercaseString;
21-
}
22+
class UnauthorizedState: State {
23+
func isAuthorized(context: Context) -> Bool { return false }
24+
func userId(context: Context) -> String? { return nil }
2225
}
2326

24-
class LowerCaseStrategy: PrintStrategy{
25-
func printString(string:String)->String{
26-
return string.lowercaseString;
27-
}
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 }
2832
}

0 commit comments

Comments
 (0)