状态模式
定义
允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
类图
代码示例
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| class State { public: virtual void insertQuarter() = 0; virtual void ejectQuarter() = 0; virtual void turnCrank() = 0; virtual void dispense() = 0; };
class NoQuarterState :public State { private: GumballMachine* machine; public: NoQuarterState(GumballMachine* m) :machine(m) {
}
void insertQuarter() { cout << "you insert a coin" << endl; machine->setState(machine->getHasQuarterState()); }
void ejectQuarter() { cout << "Error" << endl; }
void turnCrank() { cout << "Error" << endl; }
void dispense() { cout << "Error" << endl; } };
class HasQuarterState :public State { private: GumballMachine* machine; public: HasQuarterState(GumballMachine* m) :machine(m) {
}
void insertQuarter() { cout << "Error" << endl; }
void ejectQuarter() { cout << "coin return" << endl; machine->setState(machine->getNoQuarterState()); }
void turnCrank() { cout << "turn crank" << endl; machine->setState(machine->getSoldState()); }
void dispense() { cout << "Error" << endl; } };
class SoldState :public State { private: GumballMachine* machine; public: SoldState(GumballMachine* m) :machine(m) {
}
void insertQuarter() { cout << "Error" << endl; }
void ejectQuarter() { cout << "Error" << endl; }
void turnCrank() { cout << "Error" << endl; }
void dispense() { machine->releaseBall(); if (machine->getCount() > 0){ machine->setState(machine->getNoQuarterState()); } else { machine->setState(machine->getSoldOutState()); } } };
class SoldOutState :public State { private: GumballMachine* machine; public: SoldOutState(GumballMachine* m) :machine(m) {
}
void insertQuarter() { cout << "Sold out" << endl; }
void ejectQuarter() { cout << "Sold out" << endl; }
void turnCrank() { cout << "Sold out" << endl; }
void dispense() { cout << "Sold out" << endl; } };
class GumballMachine { private: State* noQuarterState; State* hasQuarterState; State* soldState; State* soldoutState; State* state; int count = 0;
public: GumballMachine(int num) : noQuarterState(new NoQuarterState(this)), hasQuarterState(new HasQuarterState(this)), soldState(new SoldState(this)), soldoutState(new SoldOutState(this)), state(noQuarterState), count(num) {
}
void insertQuarter() { state->insertQuarter(); }
void ejectQuarter() { state->ejectQuarter(); }
void turnCrank() { state->turnCrank(); state->dispense(); }
void setState(State* s) { state = s; }
void releaseBall() { cout << "a ball comes out!" << endl; if (count != 0) { --count; } }
State* getNoQuarterState() { return noQuarterState; } State* getHasQuarterState() { return hasQuarterState; } State* getSoldState() { return soldState; } State* getSoldOutState() { return soldoutState; } int getCount() { return count; } };
|
理解
将一堆if的状态判断语句换成一堆状态类,每一个类代表一个状态,一个类只处理自己的状态,将代码分开,不容易出错,且容易扩展。