State Pattern模式(状态模式)

2020-12-25  本文已影响0人  涅槃快乐是金

状态模式(State Pattern):用类来表示状态。这种类型的设计模式属于行为型模式。

介绍

示例

State.ts 表示金库状态的接口
import { Context } from "./Context";

export interface State {
    doClock(context: Context, hour: number): void;
    doUse(context: Context): void;
    doAlarm(context: Context): void;
    doPhone(context: Context): void;
}
DayState.ts 表示白天状态的类,实现了State的接口
import { Context } from "./Context";
import { State } from "./State";
import NightState from "./NightState";

export default class DayState implements State {
    private static singleton: DayState = new DayState();
    public static getInstance(): DayState {
        return this.singleton;
    }
    doClock(context: Context, hour: number): void {
        if (hour < 9 || hour >= 17) {
            context.changeState(NightState.getInstance());
        }
    }
    doUse(context: Context): void {
        context.recordLog("使用金库(白天)");
    }
    doAlarm(context: Context): void {
        context.callSecurityCenter("按下警铃(白天)")
    }
    doPhone(context: Context): void {
        context.callSecurityCenter("正常通话(白天)")
    }

    public toString(): string {
        return "[白天]"
    }
}
NightState.ts 表示晚上状态的类,实现了State的接口
import { Context } from "./Context";
import { State } from "./State";
import DayState from "./DayState";

export default class NightState implements State {
    private static singleton: NightState = new NightState();
    public static getInstance(): NightState {
        return this.singleton;
    }
    doClock(context: Context, hour: number): void {
        if (hour >= 9 && hour < 17) {
            context.changeState(DayState.getInstance())
        }
    }
    doUse(context: Context): void {
        context.recordLog("紧急:晚上使用金库!");
    }
    doAlarm(context: Context): void {
        context.callSecurityCenter("按下警铃(晚上)")
    }
    doPhone(context: Context): void {
        context.callSecurityCenter("晚上的通话录音")
    }

    public toString(): string {
        return "[晚上]"
    }
}
Context.ts 表示管理金库的状态,并与报警中心联系的接口
import { State } from "./State";

export interface Context {
    setClock(hour: number): void;
    changeState(state: State): void;
    callSecurityCenter(msg: string): void;
    recordLog(msg: string): void;
}
Safe.ts 实现了Context接口
import { Context } from "./Context";
import { State } from "./State";
import DayState from "./DayState";

export default class Safe implements Context {
    public state: State = DayState.getInstance();

    private clock: number;
    public setClock(hour: number): void {
        this.clock = hour;
        console.log(`当前时间是${hour}`);
        this.state.doClock(this, this.clock);
    }

    public changeState(state: State): void {
        console.log(`从${this.state}状态变为了${state}状态`);
        this.state = state;
    }
    public callSecurityCenter(msg: string): void {
        console.log(`call!${msg}`);
    }
    public recordLog(msg: string): void {
        console.log(`record....!${msg}`);
    }
}
index.ts
import Safe from "./Safe";

const safe: Safe = new Safe();
safe.setClock(10);
safe.state.doUse(safe);
safe.state.doAlarm(safe);
safe.state.doPhone(safe);


safe.setClock(20);
safe.state.doUse(safe);
safe.state.doAlarm(safe);
safe.state.doPhone(safe);
result
当前时间是10
record....!使用金库(白天)
call!按下警铃(白天)
call!正常通话(白天)
当前时间是20
从[白天]状态变为了[晚上]状态
record....!紧急:晚上使用金库!
call!按下警铃(晚上)
call!晚上的通话录音

类图

类图

角色

上一篇下一篇

猜你喜欢

热点阅读