#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. Nexus配置

    1.可以为maven项目单独配置nexus路径 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  2. [ActionScript 3.0] AS3动态改变注册点

    package { import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import fl ...

  3. angularjs ng-select ng-options 默认选中项.

    <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="utf- ...

  4. 菜鸟-手把手教你把Acegi应用到实际项目中(7)-缓存用户信息

    首先讲讲EhCache.在默认情况下,即在用户未提供自身配置文件ehcache.xml或ehcache-failsafe.xml时,EhCache会依据其自身Jar存档包含的ehcache-fails ...

  5. Android——主流分辨率

    VGA:480*640 QVGA:240*320 HVGA:320*480 WVGA:480*800 FWVGA:480*854 IntelHaxm.exe  模拟器加速器

  6. <%%>与<%#%>与<%=%>

    在asp.net中经常出现包含这种形式<%%>的html代码,总的来说包含下面这样几种格式: 一. <%%> 这种格式实际上就是和asp的用法一样的,只是asp中里面是vbsc ...

  7. 使用kendoui对grid指定行变色

    关键点在于绑定数据源后进行判断,可直接获取当前绑定对象的属性 dataBound: function () { dataView = this.dataSource.view(); ; i < ...

  8. CODESOFT 2015中的条形码对象该如何创建

     CODESOFT条码设计软件提供了大量适应行业要求的符号,以及创建二维条形码的选项.用户可以通过条形码对话框选择符号.定义其属性以及输入要编码的消息.下面小编带大家具体学习下如何在CODESOFT ...

  9. 代码生成器(CodeBuilder) 2 正式发布

    CodeBuilder是一个通过获取数据库表和字段定义,通过模板转换生成三层结构.实体模型等代码的工具. CodeBuilder第一版距今已过去4个年头了,第一版做的功能繁多,体积庞大,但是用起来不太 ...

  10. 关于java.lang.String理解中的一些难点

    最近温习java的一些基础知识,发现以往对String对象认识上的一些不足.特汇总如下,主要是帮助记忆,如能对其他朋友有些启发,不胜欣喜. String在JVM中内存驻留问题 JVM的常量区(Cons ...