This file contains hidden or 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
class GoodGuy{ | |
private var characterState : CharacterState? | |
func setState(cs : CharacterState){ | |
self.characterState = cs | |
characterState!.enterState() | |
} | |
} |
This file contains hidden or 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
protocol CharacterState{ | |
func enterState() | |
} |
This file contains hidden or 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
class RunningState : CharacterState{ | |
func enterState() { | |
print("Entering running state") | |
} | |
} |
This file contains hidden or 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
let goodGuy = GoodGuy() | |
goodGuy.setState(cs: WalkingState()) | |
goodGuy.setState(cs: StandingState()) | |
goodGuy.setState(cs: RunningState()) |
This file contains hidden or 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
class BadGuy{ | |
private var characterState : CharacterState? | |
func setState(cs : CharacterState){ | |
self.characterState = cs | |
characterState!.enterState() | |
} | |
} |
This file contains hidden or 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
let goodGuy = GoodGuy() | |
let badGuy = BadGuy() | |
goodGuy.setState(cs: WalkingState()) | |
goodGuy.setState(cs: StandingState()) | |
goodGuy.setState(cs: RunningState()) | |
badGuy.setState(cs: WalkingState()) | |
badGuy.setState(cs: StandingState()) | |
badGuy.setState(cs: RunningState()) |
This file contains hidden or 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
protocol Observer{ | |
var id : Int{ get } | |
func update() | |
} |
This file contains hidden or 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
class Subject{ | |
private var observerArray = [Observer]() | |
private var _number = Int() | |
var number : Int { | |
set { | |
_number = newValue | |
notify() | |
} | |
get { |
This file contains hidden or 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
class BinaryObserver : Observer{ | |
private var subject = Subject() | |
var id = Int() | |
init(subject : Subject, id : Int) { | |
self.subject = subject | |
self.subject.attachObserver(observer: self) | |
self.id = id | |
} |
This file contains hidden or 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
let subject = Subject() | |
let binary = BinaryObserver(subject: subject, id: 1) | |
let octal = OctalObserver(subject: subject, id: 2) | |
let hex = HexaObserver(subject: subject, id: 3) | |
subject.number = 15 | |
subject.number = 2 |
OlderNewer