Skip to content

Instantly share code, notes, and snippets.

@veryjos
Created August 29, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veryjos/bfbbe34517572ad0c53c26c693ed5871 to your computer and use it in GitHub Desktop.
Save veryjos/bfbbe34517572ad0c53c26c693ed5871 to your computer and use it in GitHub Desktop.
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