状态模式(大话设计模式)C/C++版本

状态模式

C++

cpp 复制代码
#include <iostream>
using namespace std;

class State;
class ForenoonState;
class NoonState;
class AfternonnState;
class EveningState;
class SleepingState;
class RestState;
class Work;

// 抽象状态角色,负责定义各个状态有哪些行为,该抽象状态包含所有具体状态的方法。并且封装环境角色,帮助切换状态。
class State
{
public:
    virtual void writeprogram(Work *w) = 0;
    virtual ~State() = default;
};
// Context 角色,在该类内部维护一个ConcreteState子类的一个实例,可以负责具体状态的切换
class Work
{
private:
    State *current;
    double m_hour;
    bool m_finish = false;

public:
    Work();
    ~Work();
    void setHour(double hour) { m_hour = hour; }
    double getHour() { return m_hour; }

    void setFinish(bool finish) { m_finish = finish; }
    bool getFinish() { return m_finish; }
    void SetState(State *s)
    {
        delete current;
        current = s;
    }
    void WorkProgram()
    {
        current->writeprogram(this);
    }
};

// 下面都是具体的状态类,每一个具体状态类必须完成两个职责:该类本状态下要做的事情,以及如何执行到其他具体状态类的状态。
class ForenoonState : public State
{
public:
    void writeprogram(Work *w) override;
};

class NoonState : public State
{
public:
    void writeprogram(Work *w) override;
};

class AfternoonState : public State
{
public:
    void writeprogram(Work *w) override;
};

class EveningState : public State
{
public:
    void writeprogram(Work *w) override;
};

class SleepingState : public State
{ // 睡眠状态
public:
    void writeprogram(Work *w) override;
};

class RestState : public State
{ // 下班休息状态
public:
    void writeprogram(Work *w) override;
};

Work::Work()
{
    current = new ForenoonState();
}
Work::~Work()
{
    delete current;
}

void ForenoonState::writeprogram(Work *w)
{
    if (w->getHour() < 12)
    {
        cout << "当前时间:" << w->getHour() << "点,上午工作,精神百倍" << endl;
    }
    else
    {
        w->SetState(new NoonState());
        w->WorkProgram();
    }
}

void NoonState::writeprogram(Work *w)
{
    if (w->getHour() < 13)
    {
        cout << "当前时间:" << w->getHour() << "点,吃午饭,睡午觉" << endl;
    }
    else
    {
        w->SetState(new AfternoonState());
        w->WorkProgram();
    }
}

void AfternoonState::writeprogram(Work *w)
{
    if (w->getHour() < 17)
    {
        cout << "当前时间:" << w->getHour() << "点,下午状态还不错" << endl;
    }
    else
    {
        w->SetState(new EveningState());
        w->WorkProgram();
    }
}

void EveningState::writeprogram(Work *w)
{
    if (w->getFinish())
    {
        w->SetState(new RestState());
        w->WorkProgram();
    }
    else
    {
        if (w->getHour() < 21)
        {
            cout << "当前时间:" << w->getHour() << "点,加班哦,疲惫至极 " << endl;
        }
        else
        {
            w->SetState(new SleepingState());
            w->WorkProgram();
        }
    }
}

void SleepingState::writeprogram(Work *w)
{
    cout << "当前时间:" << w->getHour() << "点,不行了,睡着了 " << endl;
}

void RestState::writeprogram(Work *w)
{
    cout << "当前时间:" << w->getHour() << "点,下班回家了 " << endl;
}

int main()
{
    Work emergencyProjects;

    emergencyProjects.setHour(9);
    emergencyProjects.WorkProgram();

    emergencyProjects.setHour(10);
    emergencyProjects.WorkProgram();

    emergencyProjects.setHour(12);
    emergencyProjects.WorkProgram();

    emergencyProjects.setHour(13);
    emergencyProjects.WorkProgram();

    emergencyProjects.setHour(14);
    emergencyProjects.WorkProgram();

    emergencyProjects.setHour(17);
    emergencyProjects.WorkProgram();

    emergencyProjects.setFinish(false);

    emergencyProjects.setHour(19);
    emergencyProjects.WorkProgram();

    emergencyProjects.setHour(22);
    emergencyProjects.WorkProgram();

    return 0;
}

C

c 复制代码
#include <stdio.h>

typedef struct Work
{
    enum
    {
        FORENOON,
        NOON,
        AFTERNOON,
        EVENING,
        SLEEPING,
        REST
    } state;
    double hour;
    int finish;
} Work;

void forenoonState(Work *w);
void noonState(Work *w);
void afternoonState(Work *w);
void eveningState(Work *w);
void sleepingState(Work *w);
void restState(Work *w);

void workProgram(Work *w)
{
    switch (w->state)
    {
    case FORENOON:
        forenoonState(w);
        break;
    case NOON:
        noonState(w);
        break;
    case AFTERNOON:
        afternoonState(w);
        break;
    case EVENING:
        eveningState(w);
        break;
    case SLEEPING:
        sleepingState(w);
        break;
    case REST:
        restState(w);
        break;
    default:
        break;
    }
}

void forenoonState(Work *w)
{
    if (w->hour < 12)
    {
        printf("当前时间:%lf点,上午工作,精神百倍\n", w->hour);
    }
    else
    {
        w->state = NOON;
        workProgram(w);
    }
}

void noonState(Work *w)
{
    if (w->hour < 13)
    {
        printf("当前时间:%lf点,吃午饭,睡午觉\n", w->hour);
    }
    else
    {
        w->state = AFTERNOON;
        workProgram(w);
    }
}

void afternoonState(Work *w)
{
    if (w->hour < 17)
    {
        printf("当前时间:%lf点,下午状态还不错\n", w->hour);
    }
    else
    {
        w->state = EVENING;
        workProgram(w);
    }
}

void eveningState(Work *w)
{
    if (w->hour < 21)
    {
        printf("当前时间:%lf点,加班哦,疲惫至极\n", w->hour);
    }
    else
    {
        w->state = SLEEPING;
        workProgram(w);
    }
}

void sleepingState(Work *w)
{
    printf("当前时间:%lf点,不行了,睡着了\n", w->hour);
}

void restState(Work *w)
{
    printf("当前时间:%lf点,下班回家了\n", w->hour);
}

int main()
{
    Work emergencyProjects = {FORENOON, 0, 0};

    emergencyProjects.hour = 9;
    workProgram(&emergencyProjects);

    emergencyProjects.hour = 10;
    workProgram(&emergencyProjects);

    emergencyProjects.finish = 0;
    emergencyProjects.hour = 19;
    workProgram(&emergencyProjects);

    emergencyProjects.hour = 22;
    workProgram(&emergencyProjects);

    return 0;
}
相关推荐
诸葛悠闲30 分钟前
设计模式——桥接模式
设计模式·桥接模式
zh路西法42 分钟前
【C++决策和状态管理】从状态模式,有限状态机,行为树到决策树(一):从电梯出发的状态模式State Pattern
c++·决策树·状态模式
stm 学习ing44 分钟前
HDLBits训练5
c语言·fpga开发·fpga·eda·hdlbits·pld·hdl语言
就爱学编程2 小时前
重生之我在异世界学编程之C语言小项目:通讯录
c语言·开发语言·数据结构·算法
北国无红豆3 小时前
【CAN总线】STM32的CAN外设
c语言·stm32·嵌入式硬件
单片机学习之路3 小时前
【C语言】结构
c语言·开发语言·stm32·单片机·51单片机
brrdg_sefg4 小时前
Rust 在前端基建中的使用
前端·rust·状态模式
graceyun4 小时前
C语言初阶习题【9】数9的个数
c语言·开发语言
捕鲸叉5 小时前
C++软件设计模式之外观(Facade)模式
c++·设计模式·外观模式
小小小妮子~5 小时前
框架专题:设计模式
设计模式·框架