Skip to content

Commit 1cd561c

Browse files
committed
Added Memento Pattern
1 parent 49a71a0 commit 1cd561c

21 files changed

+305
-132
lines changed

Design-Patterns.playground.zip

1.81 KB
Binary file not shown.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
<section class="section">
1414
<h2 id="-iterator">🚧 Iterator</h2>
1515
<h2 id="-mediator">🚧 Mediator</h2>
16-
<h2 id="-memento">🚧 Memento</h2>
17-
<h2 id="-observer">👓 Observer</h2>
16+
<h2 id="-memento">💾 Memento</h2>
1817

1918
</section>
2019
</div>

Design-Patterns.playground/Documentation/section-61.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-
<p><strong>Usage:</strong></p>
14+
<p><strong><em>Usage:</em></strong></p>
1515

1616
</section>
1717
</div>

Design-Patterns.playground/Documentation/section-63.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="-state">🐉 State</h2>
14+
<h2 id="-observer">👓 Observer</h2>
1515

1616
</section>
1717
</div>

Design-Patterns.playground/Documentation/section-67.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="-strategy">💡 Strategy</h2>
14+
<h2 id="-state">🐉 State</h2>
1515

1616
</section>
1717
</div>

Design-Patterns.playground/Documentation/section-71.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>

Design-Patterns.playground/Documentation/section-75.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="-visitor">🏃 Visitor</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 78</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 80</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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,13 @@
153153
</code>
154154
<documentation relative-path="section-75.html">
155155
</documentation>
156+
<code source-file-name="section-76.swift">
157+
</code>
158+
<documentation relative-path="section-77.html">
159+
</documentation>
160+
<code source-file-name="section-78.swift">
161+
</code>
162+
<documentation relative-path="section-79.html">
163+
</documentation>
156164
</sections>
157165
</playground>
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1-
class StepCounter {
2-
var totalSteps: Int = 0 {
3-
4-
willSet(newTotalSteps) {
5-
println("About to set totalSteps to \(newTotalSteps)")
6-
}
7-
8-
didSet {
9-
10-
if totalSteps > oldValue {
11-
println("Added \(totalSteps - oldValue) steps")
12-
}
13-
}
1+
typealias Memento = Dictionary<NSObject,AnyObject>
2+
3+
/**
4+
* Originator
5+
*/
6+
class GameState {
7+
8+
var gameLevel:Int = 1
9+
var playerScore:Int = 0
10+
11+
func saveToMemeto() -> Memento { return ["gameLevel":gameLevel,"playerScore":playerScore] }
12+
13+
func restoreFromMemeto(memento:Memento) {
14+
gameLevel = memento["gameLevel"]! as Int
15+
playerScore = memento["playerScore"]! as Int
16+
}
17+
}
18+
19+
/**
20+
* Caretaker
21+
*/
22+
class CheckPoint {
23+
24+
class func saveState(memento:Memento,keyName:String = "gameState"){
25+
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
26+
defaults.setObject(memento , forKey:keyName)
27+
defaults.synchronize()
28+
}
29+
30+
class func restorePreviousState(keyName:String = "gameState") -> Memento{
31+
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
32+
return defaults.objectForKey(keyName) as Memento
1433
}
1534
}
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
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+
var gameState = GameState()
2+
gameState.gameLevel = 2
3+
gameState.playerScore = 200
4+
5+
//saves state:{gameLevel 2 playerScore 200}
6+
CheckPoint.saveState(gameState.saveToMemeto())
7+
8+
gameState.gameLevel = 3
9+
gameState.gameLevel = 250
10+
11+
//restores state:{gameLevel 2 playerScore 200}
12+
gameState.restoreFromMemeto(CheckPoint.restorePreviousState())
13+
14+
gameState.gameLevel = 4
15+
16+
//saves state - gameState2:{gameLevel 4 playerScore 200}
17+
CheckPoint.saveState(gameState.saveToMemeto(),keyName: "gameState2")
18+
19+
gameState.gameLevel = 5
20+
gameState.playerScore = 300
21+
22+
//saves state - gameState3:{gameLevel 5 playerScore 300}
23+
CheckPoint.saveState(gameState.saveToMemeto(),keyName: "gameState3")
24+
25+
//restores state - gameState2:{gameLevel 4 playerScore 200}
26+
gameState.restoreFromMemeto(CheckPoint.restorePreviousState(keyName: "gameState2"))
Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
1-
class Context {
2-
private var state: State = UnauthorizedState()
3-
4-
var isAuthorized: Bool {
5-
get { return state.isAuthorized(self) }
6-
}
7-
8-
var userId: String? {
9-
get { return state.userId(self) }
1+
class StepCounter {
2+
var totalSteps: Int = 0 {
3+
4+
willSet(newTotalSteps) {
5+
println("About to set totalSteps to \(newTotalSteps)")
6+
}
7+
8+
didSet {
9+
10+
if totalSteps > oldValue {
11+
println("Added \(totalSteps - oldValue) steps")
12+
}
13+
}
1014
}
11-
12-
func changeStateToAuthorized(#userId: String) {
13-
state = AuthorizedState(userId: userId)
14-
}
15-
16-
func changeStateToUnauthorized() {
17-
state = UnauthorizedState()
18-
}
19-
20-
21-
}
22-
23-
protocol State {
24-
func isAuthorized(context: Context) -> Bool
25-
func userId(context: Context) -> String?
26-
}
27-
28-
class UnauthorizedState: State {
29-
func isAuthorized(context: Context) -> Bool { return false }
30-
31-
func userId(context: Context) -> String? { return nil }
32-
}
33-
34-
class AuthorizedState: State {
35-
let userId: String
36-
37-
init(userId: String) { self.userId = userId }
38-
39-
func isAuthorized(context: Context) -> Bool { return true }
40-
41-
func userId(context: Context) -> String? { return userId }
4215
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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 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
Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1-
protocol PrintStrategy {
2-
func printString(string: String) -> String
3-
}
4-
5-
class Printer {
1+
class Context {
2+
private var state: State = UnauthorizedState()
63

7-
let strategy: PrintStrategy
8-
9-
func printString(string: String) -> String {
10-
return self.strategy.printString(string)
4+
var isAuthorized: Bool {
5+
get { return state.isAuthorized(self) }
116
}
12-
13-
init(strategy: PrintStrategy) {
14-
self.strategy = strategy
7+
8+
var userId: String? {
9+
get { return state.userId(self) }
1510
}
11+
12+
func changeStateToAuthorized(#userId: String) {
13+
state = AuthorizedState(userId: userId)
14+
}
15+
16+
func changeStateToUnauthorized() {
17+
state = UnauthorizedState()
18+
}
19+
20+
1621
}
1722

18-
class UpperCaseStrategy : PrintStrategy {
19-
func printString(string:String) -> String {
20-
return string.uppercaseString
21-
}
23+
protocol State {
24+
func isAuthorized(context: Context) -> Bool
25+
func userId(context: Context) -> String?
2226
}
2327

24-
class LowerCaseStrategy : PrintStrategy {
25-
func printString(string:String) -> String {
26-
return string.lowercaseString
27-
}
28+
class UnauthorizedState: State {
29+
func isAuthorized(context: Context) -> Bool { return false }
30+
31+
func userId(context: Context) -> String? { return nil }
32+
}
33+
34+
class AuthorizedState: State {
35+
let userId: String
36+
37+
init(userId: String) { self.userId = userId }
38+
39+
func isAuthorized(context: Context) -> Bool { return true }
40+
41+
func userId(context: Context) -> String? { return userId }
2842
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var lower = Printer(strategy:LowerCaseStrategy())
2-
lower.printString("O tempora, o mores!")
3-
4-
var upper = Printer(strategy:UpperCaseStrategy())
5-
upper.printString("O tempora, o mores!")
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)

0 commit comments

Comments
 (0)