当前位置: 首页 > news >正文

【30】c++设计模式——>状态模式

状态模式概述

状态模式是一种行为型设计模式,它可以让一个对象在其内部状态发生变化时更改其行为。通过将每个状态封装成一个独立的类,我们可以使状态之间互相独立,并且使得状态的切换变得更加灵活、可扩展。(多个状态之间可以相互转换)
在状态模式中,我们通常会定义一个抽象状态类(Abstract State),以及多个具体状态类(Concrete States)。每个具体状态都会实现抽象状态类中定义的各种操作,并且在需要时执行状态转换。
此外,还有一个环境类(Context),它包含了当前状态,并且在状态发生变化时调用各个状态类的方法来实现状态转换。因为这些状态类都实现了同一个接口,所以环境类不需要知道具体状态的细节,只需要调用相应的方法即可。

如何理解状态类和环境类

电视遥控器
开电视学习
关电视睡觉
静音打游戏

电视遥控器的例子

一个电视遥控器。电视遥控器有三种状态:开机状态、关机状态和静音状态。我们可以通过简单地按遥控器上的按钮来更改状态。为了实现这个功能,首先我们需要定义一个抽象状态类,该类将定义所有可能的操作:

class TVState {
public:virtual void onButtonPressed(TVRemote* remote) = 0;virtual void offButtonPressed(TVRemote* remote) = 0;virtual void muteButtonPressed(TVRemote* remote) = 0;
};

TVState中包含了三种操作:开机、关机和静音。这些操作在不同的状态下可能会有不同的实现方式,因此我们需要在具体状态类中进行实现。

// 具体状态类:开机状态
class OnState : public TVState {
public:void onButtonPressed(TVRemote* remote) override {std::cout << "The TV is already on." << std::endl;}void offButtonPressed(TVRemote* remote) override {std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());}void muteButtonPressed(TVRemote* remote) override {std::cout << "Muting the TV." << std::endl;remote->setState(new MuteState());}
};// 具体状态类:关机状态
class OffState : public TVState {
public:void onButtonPressed(TVRemote* remote) override {std::cout << "Turning on the TV." << std::endl;remote->setState(new OnState());}void offButtonPressed(TVRemote* remote) override {std::cout << "The TV is already off." << std::endl;}void muteButtonPressed(TVRemote* remote) override {std::cout << "Cannot mute the TV when it's turned off." << std::endl;}
};// 具体状态类:静音状态
class MuteState : public TVState {
public:void onButtonPressed(TVRemote* remote) override {std::cout << "Unmuting the TV." << std::endl;remote->setState(new OnState());}void offButtonPressed(TVRemote* remote) override {std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());}void muteButtonPressed(TVRemote* remote) override {std::cout << "The TV is already muted." << std::endl;}
};

在这些具体状态类中,我们重写了TVState中定义的所有操作,并且在需要时执行状态转换。例如,在开机状态下按下静音键会将遥控器的状态更改为“静音状态”。
接下来,让我们定义环境类(Context):

class TVRemote {
private:TVState* currentState;public:TVRemote() {currentState = new OffState();}void setState(TVState* state) {currentState = state;}void pressOnButton() {currentState->onButtonPressed(this);}void pressOffButton() {currentState->offButtonPressed(this);}void pressMuteButton() {currentState->muteButtonPressed(this);}
};

在环境类中,我们维护了当前状态,并且在状态发生变化时调用相应的具体状态类方法。我们还定义了三个按键操作:开机、关机和静音。
现在,我们可以使用电视遥控器来测试状态模式的实现了:

int main() {TVRemote remote;remote.pressOnButton();    // Turning on the TV.remote.pressOnButton();    // The TV is already on.remote.pressMuteButton();  // Muting the TV.remote.pressMuteButton();  // The TV is already muted.remote.pressOffButton();   // Turning off the TV.remote.pressOffButton();   // The TV is already off.remote.pressMuteButton();  // Cannot mute the TV when it's turned off.remote.pressOnButton();    // Turning on the TV.remote.pressMuteButton();  // Unmuting the TV.return 0;
}

通过上面的代码,我们可以看到当我们按下不同的键时,电视遥控器的状态会发生相应的变化。

完整代码

remote.cpp

#include "remote.h"
#include "state.h"TVRemote::TVRemote(TVState* State )
{currentState = State;
}void TVRemote::setState(TVState* state)
{currentState = state;
}void TVRemote::pressOnButton()
{currentState->onButtonPressed(this);
}void TVRemote::pressOffButton()
{currentState->offButtonPressed(this);
}void TVRemote::pressMuteButton()
{currentState->muteButtonPressed(this);
}

remote.h

#pragma once
class TVState; //这里没声明,报了一堆错class TVRemote
{
private:TVState* currentState;public:TVRemote(TVState* State);void setState(TVState* state);void pressOnButton();void pressOffButton();void pressMuteButton();
};

state.h

#pragma once
#include"remote.h"class TVState {
public:virtual void onButtonPressed(TVRemote* remote) = 0; // 开机virtual void offButtonPressed(TVRemote* remote) = 0; // 关机virtual void muteButtonPressed(TVRemote* remote) = 0; // 静音
};// 具体状态类:关机状态
class OffState : public TVState
{
public:void onButtonPressed(TVRemote* remote) override;void offButtonPressed(TVRemote* remote) override;void muteButtonPressed(TVRemote* remote) override;
};// 具体状态类:开机状态
class OnState : public TVState
{
public:void onButtonPressed(TVRemote* remote) override;void offButtonPressed(TVRemote* remote) override;void muteButtonPressed(TVRemote* remote) override;
};// 具体状态类:静音状态
class MuteState : public TVState {
public:void onButtonPressed(TVRemote* remote) override;void offButtonPressed(TVRemote* remote) override;void muteButtonPressed(TVRemote* remote) override;
};

state.cpp

#include<iostream>
#include "state.h"
#include "remote.h"// 具体状态类:关机状态
void OffState::onButtonPressed(TVRemote* remote) 
{std::cout << "Turning on the TV." << std::endl;remote->setState(new OnState());
}void OffState::offButtonPressed(TVRemote* remote) 
{std::cout << "The TV is already off." << std::endl;
}void OffState::muteButtonPressed(TVRemote* remote)
{std::cout << "Cannot mute the TV when it's turned off." << std::endl;
}// 具体状态类:开机状态
void OnState::onButtonPressed(TVRemote* remote) 
{std::cout << "The TV is already on." << std::endl;
}void OnState::offButtonPressed(TVRemote* remote) 
{std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());
}void OnState::muteButtonPressed(TVRemote* remote)
{std::cout << "Muting the TV." << std::endl;remote->setState(new MuteState());
}// 具体状态类:静音状态
void MuteState::onButtonPressed(TVRemote* remote)
{std::cout << "Unmuting the TV." << std::endl;remote->setState(new OnState());
}void MuteState::offButtonPressed(TVRemote* remote) 
{std::cout << "Turning off the TV." << std::endl;remote->setState(new OffState());
}void MuteState::muteButtonPressed(TVRemote* remote)
{std::cout << "The TV is already muted." << std::endl;
}

main.cpp

#include <iostream>
using namespace std;
#include "state.h"
#include "remote.h"int main() {TVState* off = new MuteState;TVRemote remote(off);remote.pressOnButton();    // Turning on the TV.remote.pressOnButton();    // The TV is already on.remote.pressMuteButton();  // Muting the TV.remote.pressMuteButton();  // The TV is already muted.remote.pressOffButton();   // Turning off the TV.remote.pressOffButton();   // The TV is already off.remote.pressMuteButton();  // Cannot mute the TV when it's turned off.remote.pressOnButton();    // Turning on the TV.remote.pressMuteButton();  // Unmuting the TV.return 0;
}

在这里插入图片描述

相关文章:

【30】c++设计模式——>状态模式

状态模式概述 状态模式是一种行为型设计模式&#xff0c;它可以让一个对象在其内部状态发生变化时更改其行为。通过将每个状态封装成一个独立的类&#xff0c;我们可以使状态之间互相独立&#xff0c;并且使得状态的切换变得更加灵活、可扩展。&#xff08;多个状态之间可以相…...

中文编程开发语言编程实际案例:程序控制灯电路以及桌球台球室用这个程序计时计费

中文编程开发语言编程实际案例&#xff1a;程序控制灯电路以及桌球台球室用这个程序计时计费 上图为&#xff1a;程序控制的硬件设备电路图 上图为&#xff1a;程序控制灯的开关软件截图&#xff0c;适用范围比如&#xff1a;台球厅桌球室的计时计费管理&#xff0c;计时的时候…...

【python】高斯日记

题目&#xff1a; """ 题目描述&#xff1a; 高斯出生于1777年4月30日&#xff0c;记作1777-4-30。在此基础上&#xff0c;我们希望你写一个程序&#xff0c;给定一个数字n&#xff0c;表示从高斯出生的那天算起的第n天&#xff0c;输出这一天的具体日期。例如&…...

[论文笔记]MobileBERT

引言 今天带来一篇关于量化的论文MobileBERT,题目翻译过来是:一种适用于资源有限设备的紧凑型任务无关BERT模型。模型的简称是MobileBERT,意思是作者的这个BERT模型可以部署到手机端。 本篇工作,作者提出了MobileBERT用于压缩和加速BERT模型。与原始BERT一样,MobileBERT…...

【Spring Cloud】如何确定微服务项目的Spring Boot、Spring Cloud、Spring Cloud Alibaba的版本

文章目录 1. 版本选择2. 用脚手架快速生成微服务的pom.xml3. 创建一个父工程4. 代码地址 本文描述如何确定微服务项目的Spring Boot、Spring Cloud、Spring Cloud Alibaba的版本。 1. 版本选择 我们知道Spring Boot、Spring Cloud、Spring Cloud Alibaba的版本选择一致性非常重…...

Power BI 傻瓜入门 1. 数据分析术语:Power BI风格

本章内容包括&#xff1a; 了解Power BI可以处理的不同类型的数据了解您的商业智能工具选项熟悉Power BI术语 数据无处不在。从你醒来的那一刻到你睡觉的时候&#xff0c;某个系统会代表你收集数据。即使在你睡觉的时候&#xff0c;也会产生与你生活的某些方面相关的数据。如…...

【C++和数据结构】位图和布隆过滤器

目录 一、位图 1、位图的概念 2、位图的实现 ①、基本结构 ②、set ③、reset&#xff1a; ④、test ⑤、问题&#xff1a; ⑥、位图优缺点及应用&#xff1a; ⑦、完整代码及测试 二、布隆过滤器 1、布隆过滤器的提出 2、布隆过滤器的实现 ①、基本结构 ②…...

Mybatis分页

本文主要讲解Mybatis分页相关的技术分享&#xff0c;如果觉得不错的话&#xff0c;就点个赞吧。。。。 Mybatis分页主要有2种类型&#xff1a; 一、物理分页&#xff1a; 1、定义&#xff1a; 物理分页是在数据库层面进行的分页&#xff0c;即通过SQL语句直接从数据库中查询…...

手写SVG图片

有时候QT中可能会需要一些简单的SVG图片,但是网上的质量参差不齐,想要满意的SVG图片,我们可以尝试直接手写的方法. 新建文本文档,将以下代码复制进去,修改后缀名为.svg,保存 <?xml version"1.0" encoding"utf-8"?> <svg xmlns"http://www…...

LVS负载均衡及LVS-NAT模式

一、集群概述 1.1 集群的背景 集群定义&#xff1a;为解决某个特定问题将多个计算机组合起来形成一个单系统 集群目的&#xff1a;为了解决系统的性能瓶颈 集群发展历史&#xff1a; 垂直扩展&#xff1a;向上扩展&#xff0c;增加单个机器的性能&#xff0c;即升级硬件 水…...

【Java集合类面试八】、 介绍一下HashMap底层的实现原理

文章底部有个人公众号&#xff1a;热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享&#xff1f; 踩过的坑没必要让别人在再踩&#xff0c;自己复盘也能加深记忆。利己利人、所谓双赢。 面试官&#xff1a; 介绍一下HashMap底层的…...

linux 安装操作 redis

1、redis概述和安装 1.1、安装redis 1. 下载redis 地址 https://download.redis.io/releases/ 2. 将 redis 安装包拷贝到 /opt/ 目录 3. 解压 tar -zvxf redis-6.2.1.tar.gz4. 安装gcc yum install gcc5. 进入目录 cd redis-6.2.16. 编译 make7. 执行 make install 进…...

博客后台模块续更(五)

十一、后台模块-菜单列表 菜单指的是权限菜单&#xff0c;也就是一堆权限字符串 1. 查询菜单 1.1 接口分析 需要展示菜单列表&#xff0c;不需要分页。可以针对菜单名进行模糊查询。也可以针对菜单的状态进行查询。菜单要按照父菜单id和orderNum进行排序 请求方式 请求路径…...

手写一个PrattParser基本运算解析器4: 简述iOS的编译过程

点击查看 基于Swift的PrattParser项目 iOS项目的编译过程与PrattParser解析器 前面三篇我们看到了PrattParser解析器的工作原理, 工作过程, 我们了解到PrattParser解析器实际上是模拟了编译过程中的 词法分析 、语法分析 、语义分析 、 中间代码生成 这几个编译前端过程. 那么P…...

【Java集合类面试六】、 HashMap有什么特点?

文章底部有个人公众号&#xff1a;热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享&#xff1f; 踩过的坑没必要让别人在再踩&#xff0c;自己复盘也能加深记忆。利己利人、所谓双赢。 面试官&#xff1a;HashMap有什么特点&…...

基于LSTM的天气预测 - 时间序列预测 计算机竞赛

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 机器学习大数据分析项目 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f9ff; 更多资料, 项目分享&#xff1a; https://gitee.com/dancheng-senior/po…...

SpringBoot AOP + Redis 延时双删功能实战

一、业务场景 在多线程并发情况下&#xff0c;假设有两个数据库修改请求&#xff0c;为保证数据库与redis的数据一致性&#xff0c;修改请求的实现中需要修改数据库后&#xff0c;级联修改Redis中的数据。 请求一&#xff1a;A修改数据库数据 B修改Redis数据 请求二&#xff…...

【Java集合类面试七】、 JDK7和JDK8中的HashMap有什么区别?

文章底部有个人公众号&#xff1a;热爱技术的小郑。主要分享开发知识、学习资料、毕业设计指导等。有兴趣的可以关注一下。为何分享&#xff1f; 踩过的坑没必要让别人在再踩&#xff0c;自己复盘也能加深记忆。利己利人、所谓双赢。 面试官&#xff1a;JDK7和JDK8中的HashMap有…...

el-tree 获取过滤后的树结构

正常来说element框架应该返回的&#xff0c;但实际上没有&#xff0c;只能自己处理了 递归处理&#xff0c;思路就是赋值&#xff0c;如果是自己过滤到的数据就push进去&#xff0c;不是就不要 let newCheckTree [] let tree get_tree(treeData,newCheckTree); //获取过滤…...

Windows连接SFTP服务

最近有个新需求需要通过SFTP方式连接到一个FTP中下载相关内容 1.使用命令行方式 在cmd中使用如下命令 sftp -P [port] [username]ip #示例 sftp -P 666 ftp123.123.123.123然后弹出的提示输入yes&#xff0c;再输入密码就可以了。 2.使用资源管理器方式 普通FTP可以使用资源…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

在软件开发中正确使用MySQL日期时间类型的深度解析

在日常软件开发场景中&#xff0c;时间信息的存储是底层且核心的需求。从金融交易的精确记账时间、用户操作的行为日志&#xff0c;到供应链系统的物流节点时间戳&#xff0c;时间数据的准确性直接决定业务逻辑的可靠性。MySQL作为主流关系型数据库&#xff0c;其日期时间类型的…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件

在选煤厂、化工厂、钢铁厂等过程生产型企业&#xff0c;其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进&#xff0c;需提前预防假检、错检、漏检&#xff0c;推动智慧生产运维系统数据的流动和现场赋能应用。同时&#xff0c;…...

解决Ubuntu22.04 VMware失败的问题 ubuntu入门之二十八

现象1 打开VMware失败 Ubuntu升级之后打开VMware上报需要安装vmmon和vmnet&#xff0c;点击确认后如下提示 最终上报fail 解决方法 内核升级导致&#xff0c;需要在新内核下重新下载编译安装 查看版本 $ vmware -v VMware Workstation 17.5.1 build-23298084$ lsb_release…...

LLM基础1_语言模型如何处理文本

基于GitHub项目&#xff1a;https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken&#xff1a;OpenAI开发的专业"分词器" torch&#xff1a;Facebook开发的强力计算引擎&#xff0c;相当于超级计算器 理解词嵌入&#xff1a;给词语画"…...

EtherNet/IP转DeviceNet协议网关详解

一&#xff0c;设备主要功能 疆鸿智能JH-DVN-EIP本产品是自主研发的一款EtherNet/IP从站功能的通讯网关。该产品主要功能是连接DeviceNet总线和EtherNet/IP网络&#xff0c;本网关连接到EtherNet/IP总线中做为从站使用&#xff0c;连接到DeviceNet总线中做为从站使用。 在自动…...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

vue3+vite项目中使用.env文件环境变量方法

vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量&#xff0c;这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...

基于 TAPD 进行项目管理

起因 自己写了个小工具&#xff0c;仓库用的Github。之前在用markdown进行需求管理&#xff0c;现在随着功能的增加&#xff0c;感觉有点难以管理了&#xff0c;所以用TAPD这个工具进行需求、Bug管理。 操作流程 注册 TAPD&#xff0c;需要提供一个企业名新建一个项目&#…...