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
| 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
| 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
| 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() | |
| 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 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
| 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 GoodGuy{ | |
| private var characterState : CharacterState? | |
| func setState(cs : CharacterState){ | |
| self.characterState = cs | |
| characterState!.enterState() | |
| } | |
| } |
NewerOlder