forked from ochococo/Design-Patterns-In-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ochococo#20 from codestergit/MementoPattern
Added Memento Pattern
- Loading branch information
Showing
21 changed files
with
307 additions
and
132 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Section 78</title> | ||
<meta id="xcode-display" name="xcode-display" content="render"> | ||
<meta name="apple-mobile-web-app-capable" content="yes"> | ||
<meta name="viewport" content="width=device-width, maximum-scale=1.0"> | ||
<link rel="stylesheet" type="text/css" href="stylesheet.css"> | ||
</head> | ||
<body> | ||
<div class="content-wrapper"> | ||
<section class="section"> | ||
<p><strong>Usage:</strong></p> | ||
|
||
</section> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Section 80</title> | ||
<meta id="xcode-display" name="xcode-display" content="render"> | ||
<meta name="apple-mobile-web-app-capable" content="yes"> | ||
<meta name="viewport" content="width=device-width, maximum-scale=1.0"> | ||
<link rel="stylesheet" type="text/css" href="stylesheet.css"> | ||
</head> | ||
<body> | ||
<div class="content-wrapper"> | ||
<section class="section"> | ||
<h1 id="info">Info</h1> | ||
<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> | ||
<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> | ||
|
||
</section> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
class StepCounter { | ||
var totalSteps: Int = 0 { | ||
|
||
willSet(newTotalSteps) { | ||
println("About to set totalSteps to \(newTotalSteps)") | ||
} | ||
|
||
didSet { | ||
|
||
if totalSteps > oldValue { | ||
println("Added \(totalSteps - oldValue) steps") | ||
} | ||
} | ||
typealias Memento = Dictionary<NSObject, AnyObject> | ||
|
||
/** | ||
* Originator | ||
*/ | ||
class GameState { | ||
var gameLevel: Int = 1 | ||
var playerScore: Int = 0 | ||
|
||
func saveToMemeto() -> Memento { | ||
return ["gameLevel": gameLevel, "playerScore": playerScore] | ||
} | ||
|
||
func restoreFromMemeto(memento: Memento) { | ||
gameLevel = memento["gameLevel"]! as Int | ||
playerScore = memento["playerScore"]! as Int | ||
} | ||
} | ||
|
||
/** | ||
* Caretaker | ||
*/ | ||
class CheckPoint { | ||
class func saveState(memento: Memento, keyName: String = "gameState") { | ||
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() | ||
defaults.setObject(memento, forKey: keyName) | ||
defaults.synchronize() | ||
} | ||
|
||
class func restorePreviousState(keyName: String = "gameState") -> Memento { | ||
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() | ||
|
||
return defaults.objectForKey(keyName) as Memento | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
let stepCounter = StepCounter() | ||
stepCounter.totalSteps = 200 | ||
// About to set totalSteps to 200 | ||
// Added 200 steps | ||
stepCounter.totalSteps = 360 | ||
// About to set totalSteps to 360 | ||
// Added 160 steps | ||
stepCounter.totalSteps = 896 | ||
// About to set totalSteps to 896 | ||
// Added 536 steps | ||
var gameState = GameState() | ||
gameState.gameLevel = 2 | ||
gameState.playerScore = 200 | ||
|
||
// Saves state: {gameLevel 2 playerScore 200} | ||
CheckPoint.saveState(gameState.saveToMemeto()) | ||
|
||
gameState.gameLevel = 3 | ||
gameState.gameLevel = 250 | ||
|
||
// Restores state: {gameLevel 2 playerScore 200} | ||
gameState.restoreFromMemeto(CheckPoint.restorePreviousState()) | ||
|
||
gameState.gameLevel = 4 | ||
|
||
// Saves state - gameState2: {gameLevel 4 playerScore 200} | ||
CheckPoint.saveState(gameState.saveToMemeto(), keyName: "gameState2") | ||
|
||
gameState.gameLevel = 5 | ||
gameState.playerScore = 300 | ||
|
||
// Saves state - gameState3: {gameLevel 5 playerScore 300} | ||
CheckPoint.saveState(gameState.saveToMemeto(), keyName: "gameState3") | ||
|
||
// Restores state - gameState2: {gameLevel 4 playerScore 200} | ||
gameState.restoreFromMemeto(CheckPoint.restorePreviousState(keyName: "gameState2")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,15 @@ | ||
class Context { | ||
private var state: State = UnauthorizedState() | ||
|
||
var isAuthorized: Bool { | ||
get { return state.isAuthorized(self) } | ||
} | ||
|
||
var userId: String? { | ||
get { return state.userId(self) } | ||
class StepCounter { | ||
var totalSteps: Int = 0 { | ||
|
||
willSet(newTotalSteps) { | ||
println("About to set totalSteps to \(newTotalSteps)") | ||
} | ||
|
||
didSet { | ||
|
||
if totalSteps > oldValue { | ||
println("Added \(totalSteps - oldValue) steps") | ||
} | ||
} | ||
} | ||
|
||
func changeStateToAuthorized(#userId: String) { | ||
state = AuthorizedState(userId: userId) | ||
} | ||
|
||
func changeStateToUnauthorized() { | ||
state = UnauthorizedState() | ||
} | ||
|
||
|
||
} | ||
|
||
protocol State { | ||
func isAuthorized(context: Context) -> Bool | ||
func userId(context: Context) -> String? | ||
} | ||
|
||
class UnauthorizedState: State { | ||
func isAuthorized(context: Context) -> Bool { return false } | ||
|
||
func userId(context: Context) -> String? { return nil } | ||
} | ||
|
||
class AuthorizedState: State { | ||
let userId: String | ||
|
||
init(userId: String) { self.userId = userId } | ||
|
||
func isAuthorized(context: Context) -> Bool { return true } | ||
|
||
func userId(context: Context) -> String? { return userId } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
let context = Context() | ||
(context.isAuthorized, context.userId) | ||
context.changeStateToAuthorized(userId: "admin") | ||
(context.isAuthorized, context.userId) // now logged in as "admin" | ||
context.changeStateToUnauthorized() | ||
(context.isAuthorized, context.userId) | ||
let stepCounter = StepCounter() | ||
stepCounter.totalSteps = 200 | ||
// About to set totalSteps to 200 | ||
// Added 200 steps | ||
stepCounter.totalSteps = 360 | ||
// About to set totalSteps to 360 | ||
// Added 160 steps | ||
stepCounter.totalSteps = 896 | ||
// About to set totalSteps to 896 | ||
// Added 536 steps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
protocol PrintStrategy { | ||
func printString(string: String) -> String | ||
} | ||
|
||
class Printer { | ||
class Context { | ||
private var state: State = UnauthorizedState() | ||
|
||
let strategy: PrintStrategy | ||
|
||
func printString(string: String) -> String { | ||
return self.strategy.printString(string) | ||
var isAuthorized: Bool { | ||
get { return state.isAuthorized(self) } | ||
} | ||
init(strategy: PrintStrategy) { | ||
self.strategy = strategy | ||
|
||
var userId: String? { | ||
get { return state.userId(self) } | ||
} | ||
|
||
func changeStateToAuthorized(#userId: String) { | ||
state = AuthorizedState(userId: userId) | ||
} | ||
|
||
func changeStateToUnauthorized() { | ||
state = UnauthorizedState() | ||
} | ||
|
||
|
||
} | ||
|
||
class UpperCaseStrategy : PrintStrategy { | ||
func printString(string:String) -> String { | ||
return string.uppercaseString | ||
} | ||
protocol State { | ||
func isAuthorized(context: Context) -> Bool | ||
func userId(context: Context) -> String? | ||
} | ||
|
||
class LowerCaseStrategy : PrintStrategy { | ||
func printString(string:String) -> String { | ||
return string.lowercaseString | ||
} | ||
class UnauthorizedState: State { | ||
func isAuthorized(context: Context) -> Bool { return false } | ||
|
||
func userId(context: Context) -> String? { return nil } | ||
} | ||
|
||
class AuthorizedState: State { | ||
let userId: String | ||
|
||
init(userId: String) { self.userId = userId } | ||
|
||
func isAuthorized(context: Context) -> Bool { return true } | ||
|
||
func userId(context: Context) -> String? { return userId } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
var lower = Printer(strategy:LowerCaseStrategy()) | ||
lower.printString("O tempora, o mores!") | ||
|
||
var upper = Printer(strategy:UpperCaseStrategy()) | ||
upper.printString("O tempora, o mores!") | ||
let context = Context() | ||
(context.isAuthorized, context.userId) | ||
context.changeStateToAuthorized(userId: "admin") | ||
(context.isAuthorized, context.userId) // now logged in as "admin" | ||
context.changeStateToUnauthorized() | ||
(context.isAuthorized, context.userId) |
Oops, something went wrong.