HeadFirstデザパタ 第10章 State をC++で実装

自分のお勉強用 : HeadFirstデザパタ 第10章 State をC++で適当に実装

おさらい。
とりあえずStateAとBを順繰りに入れ替えるようなやつ。
お手本を真面目に写経するのは冗長なのでパス。

ファイル一覧

  1. State.h : インターフェイス
  2. StateA.h と StateA.cpp : 実装
  3. State.h と StateB.cpp : 実装
  4. GumballMachine.h と cpp
  5. state_cpp.cpp : メインクラス

実装

State.h : インターフェイス

  1. #ifndef STATE_H
  2. #define STATE_H
  3. class State
  4. {
  5. public:
  6. virtual ~State() {}
  7. virtual void methodA() = 0;
  8. virtual void methodB() = 0;
  9. };
  10. #endif // !STATE_H

StateA.h

  1. #ifndef STATE_A_H
  2. #define STATE_A_H
  3. #include "State.h"
  4. #include "GumballMachine.h"
  5. class StateA : public State
  6. {
  7. friend class GumballMachine;
  8. public:
  9. StateA(GumballMachine* gm):gm_(gm) {}
  10. virtual void methodA() ;
  11. virtual void methodB() ;
  12. private:
  13. GumballMachine* gm_;
  14. };
  15. #endif // !STATE_A_H

StateA.cpp

  1. #include "StateA.h"
  2. #include
  3. using namespace std;
  4. void StateA::methodA()
  5. {
  6. cout << "StateA::MethodA" << endl;
  7. }
  8. void StateA::methodB()
  9. {
  10. cout << "StateA::MethodB" << endl;
  11. gm_->setState( gm_->getStateB() );
  12. cout << "StateChange : A -> B" << endl;
  13. }

StateB.h

  1. #ifndef STATE_B_H
  2. #define STATE_B_H
  3. #include "State.h"
  4. #include "GumballMachine.h"
  5. class StateB : public State
  6. {
  7. friend class GumballMachine;
  8. public:
  9. StateB(GumballMachine* gm):gm_(gm) {}
  10. virtual void methodA() ;
  11. virtual void methodB() ;
  12. private:
  13. GumballMachine* gm_;
  14. };
  15. #endif // !STATE_B_H

StateB.cpp

  1. #include "StateB.h"
  2. #include
  3. using namespace std;
  4. void StateB::methodA()
  5. {
  6. cout << "BBB::AAA" << endl;
  7. }
  8. void StateB::methodB()
  9. {
  10. cout << "BBB::BBB" << endl;
  11. gm_->setState( gm_->getStateA() );
  12. cout << "StateChange : B -> A" << endl;
  13. }

GumballMachine.h

  1. #ifndef GUMBALL_MACHINE_H
  2. #define GUMBALL_MACHINE_H
  3. #include "State.h"
  4. class GumballMachine
  5. {
  6. public:
  7. GumballMachine();
  8. ~GumballMachine();
  9.  
  10. void eventA() { state_->methodA(); }
  11. void eventB() { state_->methodB(); }
  12.  
  13. void setState(State* s) { state_ = s; }
  14. State* getStateA() { return sa; }
  15. State* getStateB() { return sb; }
  16.  
  17. private:
  18. State* state_;
  19.  
  20. State* sa;
  21. State* sb;
  22. };
  23. #endif // !GUMBALL_MACHINE_H

GumballMachine.cpp

  1. #include "GumballMachine.h"
  2. #include "StateA.h"
  3. #include "StateB.h"
  4.  
  5. GumballMachine::GumballMachine() {
  6. sa = new StateA(this);
  7. sb = new StateB(this);
  8.  
  9. state_ = sa;
  10. }
  11.  
  12. GumballMachine::~GumballMachine()
  13. {
  14. delete sa;
  15. delete sb;
  16. }

state_cpp.cpp

  1. #include "GumballMachine.h"
  2. int main()
  3. {
  4. GumballMachine gm;
  5.  
  6. for (int i = 0; i < 3; i++) {
  7. gm.eventA();
  8. gm.eventB();
  9. }
  10. }

所感

ステータス周りはFlyweight的な意味でStaticでやってもよいし、
Stateのメソッド部分はコマンドパターンを当ててもよいね。

コメント

このブログの人気の投稿

windows10 で nvidia のグラボのcode43現象を解決した

Java : processbuilder 標準出力 タイムアウト

GTX560Ti がおかしい(code 43が出る)(2018年)→解決しました(2019)