状态模式对有状态的对象,把复杂的“判断逻辑”提取到不同的状态对象中,允许状态对象在其内部状态发生改变时改变其行为。
状态模式是一种对象行为型模式,其主要优点如下。
- 状态模式将与特定状态相关的行为局部化到一个状态中,并且将不同状态的行为分割开来,满足“单一职责原则”。
- 减少对象间的相互依赖。将不同的状态引入独立的对象中会使得状态转换变得更加明确,且减少对象间的相互依赖。
- 有利于程序的扩展。通过定义新的子类很容易地增加新的状态和转换。
状态模式的主要缺点如下。
- 状态模式的使用必然会增加系统的类与对象的个数。
- 状态模式的结构与实现都较为复杂,如果使用不当会导致程序结构和代码的混乱。
UML:
示例:
以仓库里的无人仓储物流机器人(AGV)为例,设计AGV的工作模式时,为AGV定义3种状态:空闲状态(接收任务),工作状态(接收新任务后放入任务队列等待执行),故障状态(不接收任何任务),代码实现如下:
1 #include <iostream> 2 3 using namespace std; 4 5 //命令类,传递到状态中执行 6 class Order{ 7 private: 8 bool accepted; 9 public: 10 string orderId; 11 Order(string _id){ 12 orderId = _id; 13 accepted = false; 14 } 15 void accept(){ 16 accepted = 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 //具体状态类 35 class StateRunning:public State{ 36 public: 37 using State::State; 38 virtual void handleNewOrder(Order* order){ 39 cout << name << "忙碌中,将任务添加到任务序列" << order->orderId << endl; 40 order->accept(); 41 } 42 }; 43 44 class StateWaiting:public State{ 45 public: 46 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 <<"故障,不接收任务" << 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实现如下:
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 extends State { 37 38 public StateRunning(String name){ 39 super(name); 40 } 41 42 @Override 43 public void handleNewOrder(Order order) { 44 System.out.println(name + "忙碌中,将任务添加到任务序列" + order.getName()); 45 order.accept(); 46 } 47 } 48 49 public class StateWaiting extends State { 50 51 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 }
输出:
running忙碌中,将任务添加到任务序列1 waiting立即执行任务2 fault故障,不接收任务
原文地址:https://www.cnblogs.com/Asp1rant/p/11332748.html
时间: 2024-10-01 00:29:05