#include <iostream>

using namespace std;

#define DESTROY_POINTER(ptr) if (ptr) { delete ptr; ptr = NULL; }

class Context;

class DbState
{
public:
DbState(Context* pContext) { m_pContext = pContext; }
virtual void Open()=;
virtual void Close()=;
virtual void Read()=;
virtual void Write()=; protected:
Context* m_pContext;
}; class OpenState : public DbState
{
public:
OpenState(Context* pContext) : DbState(pContext) {} void Open() { cout<<"Already open!"<<endl; }
void Close();
void Read() { cout<<"Finish Reading"<<endl; }
void Write() { cout<<"Finish Writing"<<endl; }
}; class CloseState : public DbState
{
public:
CloseState(Context* pContext) : DbState(pContext) {} void Open();
void Close() { cout<<"Already Closing"<<endl; }
void Read() { cout<<"Already Closing, Can't read"<<endl; }
void Write() { cout<<"Already Closing, Can't write"<<endl; }
}; class ReadingState : public DbState
{
public:
ReadingState(Context* pContext) : DbState(pContext) {} void Open() { cout<<"Already Open"<<endl; }
void Close();
void Read() { cout<<"Reading, Try again"<<endl; }
void Write() { cout<<"Reading, Can't write"<<endl; }
}; class WritingState : public DbState
{
public:
WritingState(Context* pContext) : DbState(pContext) {} void Open() { cout<<"Already Open"<<endl; }
void Close();
void Read() { cout<<"Writing, Can't read"<<endl; }
void Write() { cout<<"Writing, Try again"<<endl; }
}; class BusyState : public DbState
{
public:
BusyState(Context* pContext) : DbState(pContext) {} void Open() { cout<<"Already Open"<<endl; }
void Close() { cout<<"Busy, Can't Close"<<endl; }
void Read() { cout<<"Busy, Can't read"<<endl; }
void Write() { cout<<"Busy, Can't write"<<endl; }
}; class Context
{
public:
Context() : m_pState(NULL) {}
~Context() { DESTROY_POINTER(m_pState); } void SetState(DbState* pState) { DESTROY_POINTER(m_pState); m_pState = pState; } void Open() { m_pState->Open(); }
void Close() { m_pState->Close(); }
void Read() { m_pState->Read(); }
void Write() { m_pState->Write(); } private:
DbState* m_pState;
}; void OpenState::Close() { cout<<"Finish closing"<<endl; m_pContext->SetState(new CloseState(m_pContext)); }
void CloseState::Open() { cout<<"Finish Opening"<<endl; m_pContext->SetState(new OpenState(m_pContext)); }
void ReadingState::Close() { cout<<"Finish Closing"<<endl; m_pContext->SetState(new CloseState(m_pContext)); }
void WritingState::Close() { cout<<"Finish Closing"<<endl; m_pContext->SetState(new CloseState(m_pContext)); } int main(int argc, char *argv[])
{
Context context;
context.SetState(new OpenState(&context));
context.Open();
context.Close(); context.Open();
context.Read();
context.Write(); context.Close();
context.Write(); context.Open();
context.Write(); return ;
}

State的更多相关文章

  1. 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...

  2. react+redux教程(五)异步、单一state树结构、componentWillReceiveProps

    今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...

  3. 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)

    说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...

  4. 2015年软件测试STATE报告

    STATE OF TESTING 2015 Report 测试职业的地理位置分配 大部分有5年以上工作经验 大部分是Test Leader   测试工程师角色   测试工程师怎么工作的? 测试中的软件 ...

  5. React Native props & state

    今天又敲了一丁点代码,看了一下props和state的用法 原本以为state只是一个状态,但是又阅读了一下原文,才知道state是一组状态,这些状态是开发者自己定义的,都统一在state这个大类底下 ...

  6. React Native知识11-Props(属性)与State(状态)

    一:Props(属性) 大多数组件在创建时就可以使用各种参数来进行定制.用于定制的这些参数就称为props(属性).props是在父组件中指定,而且一经指定,在被指定的组件的生命周期中则不再改变 通过 ...

  7. Neural Pathways of Interaction Mediating the Central Control of Autonomic Bodily State 自主神经系统-大脑调节神经通路

    Figure above: Critchley H D, Harrison N A. Visceral influences on brain and behavior[J]. Neuron, 201 ...

  8. React state的使用

    相对于angular.js的双向数据绑定,React 可以使用State来实现. React 里,只需更新组件的 state,然后根据新的 state 重新渲染用户界面(不要操作 DOM). this ...

  9. html5 历史管理onhashchange和state

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. ORA-01502: index 'INDEX_NAME' or partition of such index is in unusable state

    ORA-01502: index 'INDEX_NAME' or partition of such index is in unusable state 原因: 这个错误一般是因为索引状态为UNUS ...

随机推荐

  1. c#程序中使用"like“查询access数据库语句的问题

    在写使用access数据库的c#程序过程中,遇到各种莫名奇妙的问题.例如使用"like"进行模糊查询,在access查询视图中要使用"*"做模糊匹配(sql中是 ...

  2. 全文检索引擎Solr系列—–全文检索基本原理

    场景:小时候我们都使用过新华字典,妈妈叫你翻开第38页,找到“坑爹”所在的位置,此时你会怎么查呢?毫无疑问,你的眼睛会从38页的第一个字开始从头至尾地扫描,直到找到“坑爹”二字为止.这种搜索方法叫做顺 ...

  3. 解决脱离rails使用activerecord报错 NameError: uninitialized constant ActiveRecord::Migrator::Zlib

    上下文说明 原本系统是15.10,无奈只支持1年,所以今天升级16.04,环境答好后运行rake migratte报错 task :default => :migrate desc 'Run m ...

  4. compass项目监控文件报 /usr/bin/env 找不到文件

    1 找到ruby执行文件目录 $ wherris ruby ruby: /usr/lib/ruby /home/rudy/.rbenv/shims/ruby 2 设置软链接 sudo ln -s /h ...

  5. java基础-final

  6. rhel5 新建用户提示:the home directory already exists.

    rhel5 新建用户提示:the home directory already exists.(as4不存在这个问题) 环境如下: [oracle@rhel5 ~]$ df -hFilesystem  ...

  7. Python安装Selenium3

    概述 2016.10.13,Selenium3.0正式发布,官方说明如下: The major change in Selenium 3.0 is we're removing the origina ...

  8. Orchard官方文档翻译(十) 管理Widgets

    原文地址:http://docs.orchardproject.net/Documentation/Managing-widgets 想要查看文档目录请用力点击这里 最近想要学习了解orchard,但 ...

  9. 对iframe跨域通信的封装

    github源码:https://github.com/boycy815/topProxy 为了偷懒所以依赖了Kissy:http://docs.kissyui.com/ 用法举例:需求是在http: ...

  10. infoWindowRenderer之个人见解

    在地图上以类似于提示框的方式显示查询信息,类似于arcmap中单击图层查看属性 由于Class Graphic有infoWindowRenderer : IFactory这个属性 (注:infoWin ...