Skip to content

Commit

Permalink
Merge pull request ochococo#20 from codestergit/MementoPattern
Browse files Browse the repository at this point in the history
Added Memento Pattern
  • Loading branch information
ochococo committed Oct 24, 2014
2 parents 49a71a0 + afd9772 commit 12d218c
Show file tree
Hide file tree
Showing 21 changed files with 307 additions and 132 deletions.
Binary file modified Design-Patterns.playground.zip
Binary file not shown.
3 changes: 1 addition & 2 deletions Design-Patterns.playground/Documentation/section-59.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<section class="section">
<h2 id="-iterator">🚧 Iterator</h2>
<h2 id="-mediator">🚧 Mediator</h2>
<h2 id="-memento">🚧 Memento</h2>
<h2 id="-observer">👓 Observer</h2>
<h2 id="-memento">💾 Memento</h2>

</section>
</div>
Expand Down
2 changes: 1 addition & 1 deletion Design-Patterns.playground/Documentation/section-61.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<div class="content-wrapper">
<section class="section">
<p><strong>Usage:</strong></p>
<p><strong><em>Usage:</em></strong></p>

</section>
</div>
Expand Down
2 changes: 1 addition & 1 deletion Design-Patterns.playground/Documentation/section-63.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<div class="content-wrapper">
<section class="section">
<h2 id="-state">🐉 State</h2>
<h2 id="-observer">👓 Observer</h2>

</section>
</div>
Expand Down
2 changes: 1 addition & 1 deletion Design-Patterns.playground/Documentation/section-67.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<div class="content-wrapper">
<section class="section">
<h2 id="-strategy">💡 Strategy</h2>
<h2 id="-state">🐉 State</h2>

</section>
</div>
Expand Down
2 changes: 1 addition & 1 deletion Design-Patterns.playground/Documentation/section-71.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<div class="content-wrapper">
<section class="section">
<h2 id="-visitor">🏃 Visitor</h2>
<h2 id="-strategy">💡 Strategy</h2>

</section>
</div>
Expand Down
4 changes: 1 addition & 3 deletions Design-Patterns.playground/Documentation/section-75.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<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>
<h2 id="-visitor">🏃 Visitor</h2>

</section>
</div>
Expand Down
19 changes: 19 additions & 0 deletions Design-Patterns.playground/Documentation/section-77.html
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>
21 changes: 21 additions & 0 deletions Design-Patterns.playground/Documentation/section-79.html
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>
8 changes: 8 additions & 0 deletions Design-Patterns.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,13 @@
</code>
<documentation relative-path="section-75.html">
</documentation>
<code source-file-name="section-76.swift">
</code>
<documentation relative-path="section-77.html">
</documentation>
<code source-file-name="section-78.swift">
</code>
<documentation relative-path="section-79.html">
</documentation>
</sections>
</playground>
46 changes: 33 additions & 13 deletions Design-Patterns.playground/section-60.swift
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
}
}
36 changes: 26 additions & 10 deletions Design-Patterns.playground/section-62.swift
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"))
53 changes: 13 additions & 40 deletions Design-Patterns.playground/section-64.swift
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 }
}
16 changes: 10 additions & 6 deletions Design-Patterns.playground/section-66.swift
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
54 changes: 34 additions & 20 deletions Design-Patterns.playground/section-68.swift
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 }
}
11 changes: 6 additions & 5 deletions Design-Patterns.playground/section-70.swift
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)
Loading

0 comments on commit 12d218c

Please sign in to comment.