/** State is a behavioral design pattern that lets
 * an object alter its behavior when its internal
 * state changes. It appears as if the object changed
 * its class.
 */ abstract class State { abstract name : string ; protected context : Context = new Context ( this ) ; public setContext ( context ) { this . context = context ; } public abstract handle ( ) ; } export class Context { private state : State ; constructor ( state ) { this . state = state ; this . state . setContext ( this ) ; } public getState ( ) { return this . state . name ; } public transitionTo ( state ) { this . state = state ; this . state . setContext ( this ) ; } public request ( ) { this . state . handle ( ) ; } } export class ConcreteStateA extends State { name : string = 'ConcreteStateA' ; public handle ( ) { const state = new ConcreteStateB ( ) ; this . context . transitionTo ( state ) ; } } export class ConcreteStateB extends State { name : string = 'ConcreteStateB' ; public handle ( ) { const state = new ConcreteStateA ( ) ; this . context . transitionTo ( state ) ; } }