状态模式(大话设计模式)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;
}
相关推荐
茶猫_2 小时前
力扣面试题 - 25 二进制数转字符串
c语言·算法·leetcode·职场和发展
ö Constancy3 小时前
Linux 使用gdb调试core文件
linux·c语言·vim
lb36363636363 小时前
介绍一下strncmp(c基础)
c语言·知识点
wellnw3 小时前
[linux] linux c实现共享内存读写操作
linux·c语言
小白不太白9504 小时前
设计模式之 模板方法模式
java·设计模式·模板方法模式
色空大师5 小时前
23种设计模式
java·开发语言·设计模式
闲人一枚(学习中)5 小时前
设计模式-创建型-建造者模式
java·设计模式·建造者模式
博风5 小时前
设计模式:6、装饰模式(包装器)
设计模式
A_cot5 小时前
理解设计模式与 UML 类图:构建稳健软件架构的基石
microsoft·设计模式·简单工厂模式·工厂方法模式·uml
君败红颜5 小时前
设计模式之创建模式篇
设计模式