我学会了,状态模式

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

前言

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

状态模式

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

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

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()
}
目录
相关文章
|
人工智能 机器人 API
AppFlow:无代码部署Dify作为钉钉智能机器人
本文介绍如何通过计算巢AppFlow完成Dify的无代码部署,并将其配置到钉钉中作为智能机器人使用。首先,在钉钉开放平台创建应用,获取Client ID和Client Secret。接着,创建消息卡片模板并授予应用发送权限。然后,使用AppFlow模板创建连接流,配置Dify鉴权凭证及钉钉连接凭证,完成连接流的发布。最后,在钉钉应用中配置机器人,发布应用版本,实现与Dify应用的对话功能。
3780 7
AppFlow:无代码部署Dify作为钉钉智能机器人
|
机器学习/深度学习 自然语言处理 测试技术
社区供稿 | 封神榜团队揭秘大模型训练秘密:以数据为中心
近一年来,各种各样的开源和闭源的大语言模型,不断在多个中文英文的测试基准中刷新着记录。然而,大语言模型的开发仍然面临诸多挑战,比如从头开始训练大语言模型的高昂成本,以及继续预训练导致的灾难性遗忘等等。尽管许多研究致力于解决这些问题,但一个重要而且实际的限制是,许多研究过于追求扩大模型规模,没有全面分析和优化预训练数据在训练大语言模型过程中的使用。
|
运维 负载均衡 安全
阿里云通用安全设施配置
1. 概述:  阿里云提供了 DDOS 高防、web 应用防火墙、堡垒机、云安全中心(态势感知)等安全设施,其它产品比如 负载均衡SLB, 账号体系 RAM,RDS 数据库等提供访问控制,来维护应用的安全。
4395 0
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16899 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1237 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2329 7