1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| class IContext; class IState { public: virtual ~IState() {} virtual void doColock(IContext *ctx, int hour) = 0; virtual void doUse(IContext *ctx) = 0; virtual void doAlarm(IContext *ctx) = 0; virtual void doPhone(IContext *ctx) = 0; };
class IContext { public: enum class ActionType { Use, Alarm, Phone }; virtual ~IContext() {} virtual void doAction(ActionType actionType) = 0; virtual void setClock(int hour) = 0; virtual void stateChange(IState *state) = 0; virtual void callSecurityCenter(const std::string &msg) = 0; virtual void recoderLog(const std::string &msg) = 0; };
class DayState : public IState{ public: static DayState &GetInstance(){ static DayState s; return s; } virtual void doColock(IContext *ctx, int hour) override; virtual void doUse(IContext *ctx) override{ ctx->recoderLog("Day use "); } virtual void doAlarm(IContext *ctx) override{ ctx->callSecurityCenter("Day alarm"); } virtual void doPhone(IContext *ctx) override{ ctx->callSecurityCenter("Day phone"); }
~DayState() = default; DayState(const DayState &) = delete; DayState(DayState &&) = delete; DayState &operator=(const DayState &) = delete; DayState &operator=(DayState &&) = delete;
protected: DayState() {} };
class NightState : public IState { public: static NightState &GetInstance() { static NightState s; return s; } virtual void doColock(IContext *ctx, int hour) override; virtual void doUse(IContext *ctx) override { ctx->callSecurityCenter("Night use "); } virtual void doAlarm(IContext *ctx) override { ctx->callSecurityCenter("Night alarm"); } virtual void doPhone(IContext *ctx) override { ctx->recoderLog("Night phone"); }
~NightState() = default;
NightState(const NightState &) = delete; NightState(NightState &&) = delete; NightState &operator=(const NightState &) = delete; NightState &operator=(NightState &&) = delete;
protected: NightState() {} };
void DayState::doColock(IContext *ctx, int hour) { if (hour < 9 || hour >= 17) { ctx->stateChange(&NightState::GetInstance()); } }
void NightState::doColock(IContext *ctx, int hour) { if (hour >= 9 && hour < 17) { ctx->stateChange(&DayState::GetInstance()); } }
class SafeFrame : public IContext{ public: SafeFrame(); virtual void setClock(int hour) override; virtual void doAction(ActionType actionType) override; virtual void stateChange(IState *state) override; virtual void callSecurityCenter(const std::string &msg) override; virtual void recoderLog(const std::string &msg) override;
private: IState *m_state; };
SafeFrame::SafeFrame() : m_state(&DayState::GetInstance()) {}
void SafeFrame::setClock(int hour){ std::cout << "now timw is " << hour << std::endl; m_state->doColock(this, hour); }
void SafeFrame::doAction(ActionType actionType){ switch (actionType){ case ActionType::Alarm: m_state->doAlarm(this); break; case ActionType::Phone: m_state->doPhone(this); break; case ActionType::Use: default: m_state->doUse(this); break; } }
void SafeFrame::stateChange(IState *state){ std::cout << __FUNCTION__ << std::endl; m_state = state; }
void SafeFrame::callSecurityCenter(const std::string &msg){ std::cout << __FUNCTION__ << " " << msg << std::endl; }
void SafeFrame::recoderLog(const std::string &msg){ std::cout << __FUNCTION__ << " " << msg << std::endl; }
int main() { IContext *ctx = new SafeFrame; for (int i = 0; i < 24; ++i) { ctx->setClock(i); ctx->doAction(IContext::ActionType::Use); ctx->doAction(IContext::ActionType::Phone); ctx->doAction(IContext::ActionType::Alarm); } delete ctx; return 0; }
|