博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
State Pattern(状态模式)
阅读量:5806 次
发布时间:2019-06-18

本文共 4518 字,大约阅读时间需要 15 分钟。

  • 状态(State)模式:

  当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

 

  • 状态模式的结构:

上下文环境(Context):

  它定义了客户程序需要的接口并维护一个具体状态角色的实例,将与状态相关的操作委托给当前的Concrete State对象来处理。

抽象状态(State):

  定义一个接口以封装使用上下文环境的的一个特定状态相关的行为。

具体状态(ConcreteState):

  实现抽象状态定义的接口。

 

1 //State.h 2 # ifndef _STATE_H_ 3 # define _STATE_H_ 4  5 class Context; 6  7 class State 8 { 9 public:10         State();11         virtual ~State();12         virtual void OperationInterface(Context*);13         virtual void OperationChangeState(Context*);14 protected:15         bool ChangeState(Context*, State*);16 private:17 };18 19 class ConcreteStateA:public State20 {21 public:22         ConcreteStateA();23         virtual ~ConcreteStateA();24         virtual void OperationInterface(Context*);25         virtual void OperationChangeState(Context*);26 protected:27 private:28 };29 30 class ConcreteStateB:public State31 {32 public:33         ConcreteStateB();34         virtual ~ConcreteStateB();35         virtual void OperationInterface(Context*);36         virtual void OperationChangeState(Context*);37 protected:38 private:39 };40 41 # endif
State.h
1 //State.cpp 2 # include 
3 # include "State.h" 4 # include "Context.h" 5 using namespace std; 6 7 State::State() 8 { 9 cout << "Construct State" << endl;10 }11 State::~State()12 {13 cout << "Destruct State" << endl;14 }15 void State::OperationInterface(Context* con)16 {17 cout << "State::OperationInterface" << endl;18 }19 void State::OperationChangeState(Context* con)20 {21 cout << "State::OperationChangeState" << endl;22 }23 bool State::ChangeState(Context* con, State* st)24 {25 con->ChangeState(st);26 return true;27 }28 29 ConcreteStateA::ConcreteStateA()30 {31 cout << "Construct ConcreteStateA" << endl;32 }33 ConcreteStateA::~ConcreteStateA()34 {35 cout << "Destruct ConcreteStateA" << endl;36 }37 void ConcreteStateA::OperationInterface(Context* con)38 {39 cout << "ConcreteStateA::OperationInterface" << endl;40 }41 void ConcreteStateA::OperationChangeState(Context* con)42 {43 OperationInterface(con);44 this->ChangeState(con, new ConcreteStateB());45 }46 47 ConcreteStateB::ConcreteStateB()48 {49 cout << "Construct ConcreteStateB" << endl;50 }51 ConcreteStateB::~ConcreteStateB()52 {53 cout << "Destruct ConcreteStateB" << endl;54 }55 void ConcreteStateB::OperationInterface(Context* con)56 {57 cout << "ConcreteStateB::OperationInterface" << endl;58 }59 void ConcreteStateB::OperationChangeState(Context* con)60 {61 OperationInterface(con);62 this->ChangeState(con, new ConcreteStateA());63 }
State.cpp
1 //Context.h 2 # ifndef _CONTEXT_H_ 3 # define _CONTEXT_H_ 4  5 class State; 6 class Context 7 { 8 public: 9         Context();10         Context(State*);11         ~Context();12         void OperationInterface();13         void OperationChangeState();14 protected:15 private:16         friend class State;17         bool ChangeState(State*);18         State* p_state;19 };20 21 # endif
Context.h
1 //Context.cpp 2 # include 
3 # include "Context.h" 4 # include "State.h" 5 using namespace std; 6 7 Context::Context() 8 { 9 cout << "Construct Context" << endl;10 }11 Context::Context(State* state)12 {13 this->p_state = state;14 cout << "Construct Context" << endl;15 }16 Context::~Context()17 {18 delete p_state;19 cout << "Destruct Context" << endl;20 }21 void Context::OperationInterface()22 {23 p_state->OperationInterface(this);24 }25 bool Context::ChangeState(State* state)26 {27 this->p_state = state;28 return true;29 }30 void Context::OperationChangeState()31 {32 p_state->OperationChangeState(this);33 }
Context.cpp
1 //main.cpp 2 # include 
3 # include "State.h" 4 # include "Context.h" 5 using namespace std; 6 7 int main() 8 { 9 State* st = new ConcreteStateA();10 Context* con = new Context(st);11 con->OperationInterface();12 con->OperationInterface();13 if(con != NULL)14 delete con;15 if(st != NULL)16 st = NULL;17 return 0;18 }
main.cpp

result:

blank@linux-bai:~/Projects/State> g++ -o a *.cpp && ./a

Construct State
Construct ConcreteStateA
Construct Context
ConcreteStateA::OperationInterface
ConcreteStateA::OperationInterface
Destruct ConcreteStateA
Destruct State
Destruct Context

 

转载于:https://www.cnblogs.com/blank031x/p/3872727.html

你可能感兴趣的文章
Java数据持久层框架 MyBatis之API学习九(SQL语句构建器详解)
查看>>
30分钟Git命令“从入门到放弃”
查看>>
nginx : TCP代理和负载均衡的stream模块
查看>>
MYSQL数据库间同步数据
查看>>
DevOps 前世今生 | mPaaS 线上直播 CodeHub #1 回顾
查看>>
iOS 解决UITabelView刷新闪动
查看>>
让前端小姐姐愉快地开发表单
查看>>
Dubbo笔记(四)
查看>>
Web前端JQuery入门实战案例
查看>>
java B2B2C Springboot电子商城系统- SSO单点登录之OAuth2.0 登出流程(3)
查看>>
12月26日云栖精选夜读:CDN新品发布:阿里云SCDN安全加速开放公测
查看>>
USB 通信原理
查看>>
7zZip zip RAR iOS
查看>>
date命令的详细用法!
查看>>
分布式存储ceph集群部署
查看>>
UiAutomator源码分析之UiAutomatorBridge框架
查看>>
python 开发之selenium
查看>>
Xcode3.2.5中找不到Mac OS X - Command Line Utility -...
查看>>
css的div垂直居中的方法,百分比div垂直居中
查看>>
如何理解EM算法
查看>>