Created
August 29, 2017 23:15
-
-
Save veryjos/bfbbe34517572ad0c53c26c693ed5871 to your computer and use it in GitHub Desktop.
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
interface State { | |
DoThing(): void; | |
} | |
class StandState implements State { | |
DoThing() { | |
console.log("Doing stand thing"); | |
} | |
} | |
class JabState implements State { | |
DoThing() { | |
console.log("Doing jab thing"); | |
} | |
} | |
abstract class Fighter { | |
protected StandState = StandState; | |
protected JabState = JabState; | |
private currentState: State; | |
DoState() { | |
if (this.currentState) | |
this.currentState.DoThing(); | |
} | |
SetState() { | |
this.currentState = new this.StandState(); | |
} | |
} | |
class SpecificStandState implements State { | |
DoThing() { | |
console.log("Doing specific stand thing"); | |
} | |
} | |
class SpecificFighter extends Fighter { | |
protected StandState = SpecificStandState; | |
} | |
let fighter = new SpecificFighter(); | |
fighter.SetState(); | |
fighter.DoState(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment