【设计模式】状态模式

概念

行为模式


类图


代码

cpp 复制代码
#include <iostream>

using namespace std;

class AudioPlayer;
class State {
public:
    ~State() = default;
    explicit State(AudioPlayer* audioPlayer) {
        player = audioPlayer;
    }

    virtual void ClickLock() = 0;
    virtual void ClickPlay() = 0;
    virtual void ClickNext() = 0;
    virtual void ClickPrevious() = 0;

protected:
    AudioPlayer* player;
};

class LockedState : public State {
public:
    void ClickLock() override {}
    void ClickPlay() override {}
    void ClickNext() override {}
    void ClickPrevious() override {}
};

class ReadyState : public State {};

class PlayingState : public State {};

class AudioPlayer {
public:

private:
    State* state;
};

int main(int argc, char *argv[]) {
    cout << "State pattern needs to be complemented." << endl;
    cout << "One cpp file cannot satisfy." << endl;

    return 0;
}
相关推荐
两点王爷21 小时前
Java 与前端加载 MVT 数据:从服务端切片到浏览器渲染
java·前端·状态模式
码匠许师傅21 小时前
【设计模式精讲】26.策略模式(Strategy)
c++·设计模式·策略模式·uml
Ysx1 天前
AI 问答不再"一本正经地胡说":30 轮提示词迭代,沉淀 4 条防幻觉铁律
设计模式
码匠许师傅1 天前
【设计模式精讲】25.状态模式(State)
c++·ui·设计模式·状态模式·uml
geovindu1 天前
java:Observer Pattern
java·开发语言·后端·观察者模式·设计模式·行为模式
szarron1 天前
国产手持式频谱分析仪选型攻略:TFN RC系列 vs HTOOL SA8T频谱分析仪 专业参数对比(军工/路测/调试全覆盖)
开发语言·前端·状态模式
码匠许师傅2 天前
【设计模式精讲】24.观察者模式(Observer)
c++·观察者模式·设计模式·uml
小程故事多_802 天前
从快速迭代到稳定存续,Google五大设计模式重构长效AI智能体落地逻辑
人工智能·设计模式·重构
Wang's Blog2 天前
Java框架快速入门: Spring Security+OAuth2之工程结构与开发环境配置
java·spring·状态模式
码匠许师傅3 天前
【设计模式精讲】22.中介者模式(Mediator)
c++·设计模式·软件工程·uml·中介者模式