Mode status object has a state, the complex "decision logic" extracted objects to a different state, which allows the state of the object changes behavior when its internal state changes.
State model is an object behavioral pattern, its main advantages are as follows.
- State model associated with the specific state to a localized state behavior, and the behavior of the different states separated, meet "single responsibility principle."
- Reduce interdependencies between objects. The target different states will make the introduction of a separate state transition become clearer, and reduce interdependencies between objects.
- In favor of the extension. Through new subclass definition easily add new states and transitions.
The main disadvantage of the state of mode are as follows.
- The number of classes and objects using the system inevitably increases state pattern.
- Structure and implementation of the state model are more complex, if used improperly can lead to confusion and structure of the program code.
UML:
Example:
No one at the warehouse logistics robot (AGV), for example, when the operating mode design of the AGV, AGV is defined as three states: an idle state (receiving task), work status (after receiving a new task into the task queue waiting to be executed) , fault state (not receiving any task), the code is implemented as follows:
. 1 #include <the iostream> 2 . 3 the using namespace STD; . 4 . 5 // command class, passed to the execution state . 6 class the Order { . 7 Private : . 8 BOOL accepted; . 9 public : 10 String the orderId; . 11 the Order ( String the _id) { 12 is = the orderId the _id; 13 is accepted = to false ; 14 } 15 void Accept () { 16 accepted = to true; 17 } 18 }; 19 20 //状态抽象类 21 class State{ 22 protected: 23 string name; 24 public: 25 State(string _name){ 26 name = _name; 27 } 28 virtual void handleNewOrder(Order *order) = 0; 29 string getName(){ 30 return name; 31 } 32 }; 33 34 // specific state class 35 class StateRunning: public State { 36 public : 37 [ the using State State ::; 38 is Virtual void handleNewOrder (the Order * Order) { 39 COUT << << name " busy, add tasks to the task sequence " << order-> the orderId << endl; 40 order-> Accept (); 41 is } 42 is }; 43 is 44 is class StateWaiting: public State { 45 public : 46 is the using State::State; 47 virtual void handleNewOrder(Order* order){ 48 cout << name <<"立即执行任务" << order->orderId << endl; 49 order->accept(); 50 } 51 }; 52 53 class StateFault:public State{ 54 public: 55 using State::State; 56 virtual void handleNewOrder(Order* order){ 57 cout << name <<""Failure, does not receive the task << endl; 58 } 59 }; 60 61 //环境类,AGV 62 class Agv{ 63 private: 64 State *state; 65 public: 66 void receiveNewOrder(Order* order){ 67 state->handleNewOrder(order); 68 } 69 70 void setState(State* state){ 71 this->state = state; 72 } 73 74 string getState(){ 75 return state->getName(); 76 } 77 }; 78 79 int main() 80 { 81 Agv agv; 82 State *pStateRunning = new StateRunning("running"); 83 State *pStateWaiting = new StateWaiting("waiting"); 84 State *pStateFault = new StateFault("fault"); 85 agv.setState(pStateRunning); 86 agv.receiveNewOrder(new Order("1")); 87 agv.setState(pStateWaiting); 88 agv.receiveNewOrder(new Order("2")); 89 agv.setState(pStateFault); 90 agv.receiveNewOrder(new Order("3")); 91 return 0; 92 }
Java implementation as follows:
1 public class Order { 2 3 private String name; 4 5 private boolean accepted; 6 7 public Order(String name){ 8 this.name = name; 9 accepted = false; 10 } 11 12 public void accept(){ 13 this.accepted = true; 14 } 15 16 public String getName(){ 17 return this.name; 18 } 19 } 20 21 public abstract class State { 22 23 protected String name; 24 25 public State(String name){ 26 this.name = name; 27 } 28 29 public String getName(){ 30 return name; 31 } 32 33 public void handleNewOrder(Order order){} 34 } 35 36 public class StateRunning the extends State { 37 [ 38 is public StateRunning (String name) { 39 Super (name); 40 } 41 is 42 is @Override 43 is public void handleNewOrder (the Order Order) { 44 is System.out.println (name + "busy, the task added to the task sequence "+ order.getName ()); 45 order.accept (); 46 is } 47 } 48 49 public class StateWaiting the extends State { 50 51 is public StateWaiting(String name){ 52 super(name); 53 } 54 55 @Override 56 public void handleNewOrder(Order order) { 57 System.out.println(name + "立即执行任务" + order.getName()); 58 order.accept(); 59 } 60 } 61 62 public class StateFault extends State { 63 64 public StateFault(String name){ 65 super(name); 66 } 67 68 @Override 69 public void handleNewOrder(Order order) { 70 System.out.println(name + "故障,不接收任务"); 71 } 72 } 73 74 public class Agv { 75 76 private State state; 77 78 public String getState() { 79 return state.getName(); 80 } 81 82 public void setState(State state) { 83 this.state = state; 84 } 85 86 public void receiveNewOrder(Order order){ 87 state.handleNewOrder(order); 88 } 89 } 90 91 public class Main { 92 public static void main(String[] args) { 93 Agv agv = new Agv(); 94 State stateRunning = new StateRunning("running"); 95 State stateWaiting = new StateWaiting("waiting"); 96 State stateFault = new StateFault("fault"); 97 agv.setState(stateRunning); 98 agv.receiveNewOrder(new Order("1")); 99 agv.setState(stateWaiting); 100 agv.receiveNewOrder(new Order("2")); 101 agv.setState(stateFault); 102 agv.receiveNewOrder(new Order("3")); 103 } 104 }
Output:
busy running, add the task to the task sequence. 1 Waiting immediately perform the task 2 Fault fault, no receiving task