我学会了,状态模式

简介: 状态模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

前言

状态模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

状态模式

使用场景:需要多种状态切换,并且状态的功能接口类似时,就可以使用这种模式了,比如 红黄绿灯的切换、空调制冷、制热、通风的切换。

理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了。纯状态模式,在操作类中对所有状态进行汇总,在某个状态类中去设置将要切换的下一个状态。

namespace action_mode_05_2 {

    // 接口
    interface IShowLight {
        lightPole: LightPole
        showLight(): void
    }

    // 绿灯
    class GreenLight implements IShowLight {
        lightPole: LightPole;

        constructor(lightPole: LightPole) {
            this.lightPole = lightPole
        }

        showLight(): void {
            console.log('绿灯亮,前方通行')
            // 这里面其实可以加一些判断,比如出现异常时,直接切换到红灯等等
            this.lightPole.setState(this.lightPole.yellowLight)

        }
    }

    // 黄灯
    class YellowLight implements IShowLight {
        lightPole: LightPole;

        constructor(lightPole: LightPole) {
            this.lightPole = lightPole
        }

        showLight(): void {
            console.log('黄灯亮,注意车俩')
            this.lightPole.setState(this.lightPole.redLight)
        }
    }

    // 红灯
    class RedLight implements IShowLight {
        lightPole: LightPole;

        constructor(lightPole: LightPole) {
            this.lightPole = lightPole
        }

        showLight(): void {
            console.log('红灯亮,禁止通行')
            this.lightPole.setState(this.lightPole.greenLight)
        }
    }



    // 操作类:控制内部状态的切换
    class LightPole {

        greenLight: GreenLight;
        yellowLight: YellowLight;
        redLight: RedLight;

        private currentState: IShowLight;

        stateChangeNum: number = 0


        constructor() {
            this.greenLight = new GreenLight(this)
            this.yellowLight = new YellowLight(this)
            this.redLight = new RedLight(this)

            this.currentState = this.greenLight
        }

        change() {
            this.currentState.showLight()
        }

        setState(state: IShowLight) {
            this.currentState = state
        }
    }

    // 使用
    const lightPole = new LightPole()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()

}

状态模式结合职责链模式

理解:这里的状态模式结合了职责链模式,将类、对象的行为和使用解耦了,默认情况下就是按照顺序切换状态,但是并不影响在状态类中去修改之前的顺序,也不会影响状态类之外强行变更状态。我觉得是一个很好的模式结合。

namespace action_mode_05 {

    // 接口
    interface IShowLight {
        lightPole: LightPole
        nextShowLight: IShowLight
        showLight(): void
        setNextShowLight(nextShowLight: IShowLight): IShowLight
    }

    // 绿灯
    class GreenLight implements IShowLight {
        lightPole: LightPole;
        nextShowLight!: IShowLight

        constructor(lightPole: LightPole) {
            this.lightPole = lightPole
        }

        setNextShowLight(nextShowLight: IShowLight) {
            this.nextShowLight = nextShowLight

            return nextShowLight
        }

        showLight(): void {
            if (this.nextShowLight) {
                console.log('绿灯亮,前方通行')
                this.lightPole.setState(this.nextShowLight)
            } else {
                console.log('状态异常,完犊子了')
            }
        }
    }

    // 黄灯
    class YellowLight implements IShowLight {
        lightPole: LightPole;
        nextShowLight!: IShowLight

        constructor(lightPole: LightPole) {
            this.lightPole = lightPole
        }

        setNextShowLight(nextShowLight: IShowLight) {
            this.nextShowLight = nextShowLight

            return nextShowLight
        }

        showLight(): void {
            if (this.nextShowLight) {
                console.log('黄灯亮,注意车俩')
                this.lightPole.setState(this.nextShowLight)
            } else {
                console.log('状态异常,完犊子了')
            }
        }
    }

    // 红灯
    class RedLight implements IShowLight {
        lightPole: LightPole;
        nextShowLight!: IShowLight

        constructor(lightPole: LightPole) {
            this.lightPole = lightPole
        }

        setNextShowLight(nextShowLight: IShowLight) {
            this.nextShowLight = nextShowLight
            return nextShowLight
        }

        showLight(): void {
            if (this.nextShowLight) {
                console.log('红灯亮,禁止通行')
                this.lightPole.setState(this.nextShowLight)
            } else {
                console.log('状态异常,完犊子了')
            }
        }
    }



    // 操作类:控制内部状态的切换
    class LightPole {

        greenLight: GreenLight;
        yellowLight: YellowLight;
        redLight: RedLight;

        currentState: IShowLight;


        constructor() {
            this.greenLight = new GreenLight(this)
            this.yellowLight = new YellowLight(this)
            this.redLight = new RedLight(this)

            this.currentState = this.greenLight

            // 设置状态切换的链条,在操作类内部设置
            this.currentState
                .setNextShowLight(this.yellowLight)
                .setNextShowLight(this.redLight)
                .setNextShowLight(this.greenLight)
        }

        change() {
            this.currentState.showLight()
        }

        setState(state: IShowLight) {
            this.currentState = state
        }
    }

    // 使用
    const lightPole = new LightPole()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
    lightPole.change()
}
目录
相关文章
|
存储 Kubernetes Go
听GPT 讲Istio源代码--pkg(9)
听GPT 讲Istio源代码--pkg(9)
159 0
|
运维 监控 NoSQL
一文读懂Redis哨兵
一文读懂Redis哨兵
214 0
|
图形学
[Unity UGUI]点击和长按组件
需求 游戏项目中卡片经常需要按钮/卡片的点击或者长按事件,这里提供一个好用的组件。 组件 using UnityEngine; using UnityEngine.
2221 0
|
存储 C++ Windows
带你玩转Visual Studio——带你理解多字节编码与Unicode码
目录(?)[-] 多字节字符与宽字节字符 char与wchar_t string与wstring string 与 wstring的相关转换 字符集Charcater Set与字符编码Encoding 工程里多字节与宽字符的配制 Unicode Character Se...
1636 0
|
Web App开发 JavaScript
JS window对象的top、parent、opener含义介绍 以及防止网页被嵌入框架的代码
1.top该变更永远指分割窗口最高层次的浏览器窗口。如果计划从分割窗口的最高层次开始执行命令,就可以用top变量。 2.openeropener用于在window.open的页面引用执行该window.open方法的的页面的对象。
1500 0
|
7天前
|
人工智能 运维 安全
|
5天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
607 21