状态模式(大话设计模式)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;
}
相关推荐
利刃大大13 分钟前
【Linux系统编程】二、Linux进程概念
linux·c语言·进程·系统编程
Icomi_2 小时前
【外文原版书阅读】《机器学习前置知识》1.线性代数的重要性,初识向量以及向量加法
c语言·c++·人工智能·深度学习·神经网络·机器学习·计算机视觉
apocelipes2 小时前
Linux glibc自带哈希表的用例及性能测试
c语言·c++·哈希表·linux编程
Tanecious.2 小时前
C语言--分支循环实践:猜数字游戏
android·c语言·游戏
Ronin-Lotus2 小时前
上位机知识篇---CMake
c语言·c++·笔记·学习·跨平台·编译·cmake
软工在逃男大学生4 小时前
转换算术表达式
c语言·数据结构·c++·算法
博一波4 小时前
【设计模式-行为型】迭代器模式
设计模式·迭代器模式
落羽的落羽4 小时前
【落羽的落羽 数据结构篇】算法复杂度
c语言·数据结构·算法
Oracle_66613 小时前
基于C语言的数组从入门到精通
c语言
花生_TL0000714 小时前
【C语言算法刷题】第2题 图论 dijkastra
c语言·算法·图论