允许一个对象在其内部状态改变时改变它的行为。 就是将“一个大 class + 一堆 if else”替换为“一堆小 class”。
实现
上下文 (Context) 保存了对于一个具体状态对象的引用, 并会将所有与该状态相关的工作委派给它。 上下文通过状态接口与状态对象交互, 且会提供一个设置器用于传递新的状态对象。
/**
* The Context defines the interface of interest to clients. It also maintains a
* reference to an instance of a State subclass, which represents the current
* state of the Context.
*/
class Context {
/**
* @type {State} A reference to the current state of the Context.
*/
private state: State;
constructor(state: State) {
this.transitionTo(state);
}
/**
* The Context allows changing the State object at runtime.
*/
public transitionTo(state: State): void {
console.log(`Context: Transition to ${(<any>state).constructor.name}.`);
this.state = state;
this.state.setContext(this);
}
/**
* The Context delegates part of its behavior to the current State object.
*/
public request1(): void {
this.state.handle1();
}
public request2(): void {
this.state.handle2();
}
}
状态 (State) 接口会声明特定于状态的方法。 这些方法应能被其他所有具体状态所理解, 因为你不希望某些状态所拥有的方法永远不会被调用。
/**
* The base State class declares methods that all Concrete State should
* implement and also provides a backreference to the Context object, associated
* with the State. This backreference can be used by States to transition the
* Context to another State.
*/
abstract class State {
protected context: Context;
public setContext(context: Context) {
this.context = context;
}
public abstract handle1(): void;
public abstract handle2(): void;
}
具体状态 (Concrete States) 会自行实现特定于状态的方法。 为了避免多个状态中包含相似代码, 你可以提供一个封装有部分通用行为的中间抽象类。状态对象可存储对于上下文对象的反向引用。 状态可以通过该引用从上下文处获取所需信息, 并且能触发状态转移。 ```typescript /**
- Concrete States implement various behaviors, associated with a state of the
Context. */ class ConcreteStateA extends State { public handle1(): void { console.log(‘ConcreteStateA handles request1.’); console.log(‘ConcreteStateA wants to change the state of the context.’); this.context.transitionTo(new ConcreteStateB()); }
public handle2(): void { console.log(‘ConcreteStateA handles request2.’); } }
class ConcreteStateB extends State { public handle1(): void { console.log(‘ConcreteStateB handles request1.’); }
public handle2(): void {
console.log('ConcreteStateB handles request2.');
console.log('ConcreteStateB wants to change the state of the context.');
this.context.transitionTo(new ConcreteStateA());
}
}
4. 上下文和具体状态都可以设置上下文的下个状态, 并可通过替换连接到上下文的状态对象来完成实际的状态转换。
```typescript
/**
* The client code.
*/
const context = new Context(new ConcreteStateA());
context.request1();
context.request2();
有限状态机(finite state machines, FSM)
- 你拥有一组状态,并且可以在这组状态之间进行切换
- 状态机同一时刻只能处于一种状态
- 状态机会接收一组输入或事件
- 每一个状态有一组转换,每一个转换都关联着一个输入并指向另一个状态
整个状态机可以分为:状态、输入和转换
实例
namespace Appliances
{
//
// EXPORTED TYPES
//
export class Toaster implements ToasterOperations {
private _state: ToasterState = new IdleState()
constructor() {
this.logCurrentState()
}
public insertBread(): void {
this._state = this._state.insertBread()
this.logCurrentState()
}
public pullLever(): void {
this._state = this._state.pullLever()
this.logCurrentState()
}
public ejectBread(): void {
this._state = this._state.ejectBread()
this.logCurrentState()
}
public removeBread(): void {
this._state = this._state.removeBread()
this.logCurrentState()
}
private logCurrentState(): void {
console.log(this._state)
}
}
export interface ToasterOperations {
insertBread(): void
pullLever(): void
ejectBread(): void
removeBread(): void
}
//
// HIDDEN TYPES
//
abstract class ToasterState implements ToasterOperations {
public insertBread(): ToasterState {
throw new Error("Invalid operation")
}
public pullLever(): ToasterState {
throw new Error("Invalid operation")
}
public ejectBread(): ToasterState {
throw new Error("Invalid operation")
}
public removeBread(): ToasterState {
throw new Error("Invalid operation")
}
}
class IdleState extends ToasterState {
public insertBread(): ToasterState {
return new BreadInsertedState()
}
}
class BreadInsertedState extends ToasterState {
public pullLever(): ToasterState {
return new ToastingState()
}
}
class ToastingState extends ToasterState {
public ejectBread(): ToasterState {
return new BreadEjectedState()
}
}
class BreadEjectedState extends ToasterState {
public removeBread(): ToasterState {
return new IdleState()
}
}
}
console.log("testing allowed transitions")
var toaster = new Appliances.Toaster()
toaster.insertBread()
toaster.pullLever()
toaster.ejectBread()
toaster.removeBread()
console.log("testing disallowed transitions")
var toaster2 = new Appliances.Toaster()
toaster2.pullLever()