#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. I/O地址映射

    几乎每一种外设都是通过读写设备上的寄存器来进行的,通常包括控制寄存器.状态寄存器和数据寄存器三大类,外设的寄存器通常被连续地编址.根据CPU体系结构的不同,CPU对IO端口的编址方式有两种: (1)I ...

  2. C++primer练习14.44

    编写一个简单的桌面计算器使其处理二元运算 // 14_44.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iost ...

  3. poj 2388 Who's in the Middle

    点击打开链接 Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28324   Acce ...

  4. SpringMVC 注解事务

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactio ...

  5. Linux平台块设备到字符设备(裸设备)的三种映射方式(转载)

    在Linux平台oracle rac的组建过程中,如果使用ASM+RAW的存储方式的话,由于asm不支持块设备,支持持字符访问设备,所以需要配置将Block Device Drive转变成Charac ...

  6. ASP.NET MVC开发微信(三)

  7. Java-convert between INT and STRING

    int -> String 三种写法 String s = 43 + ""; String s = String.valueOf(43); String s = Intege ...

  8. SQL Server :Stored procedures存储过程初级篇

    对于SQL Server,我是个拿来主义.很多底层的原理并不了解,就直接模仿拿着来用了,到了报错的时候,才去找原因进而逐步深入底层.我想,是每一次的报错,逼着我一点点进步的吧. 近期由于项目的原因,我 ...

  9. Filter Blue Light for Better Sleep(APP 推荐)

    Filter Blue Light for Better Sleep By Carolyn Mohr11 May, 2016 Many people like to use their phones ...

  10. 为网站加入Drupal星球制作RSS订阅源

    目前中文 Drupal 星球的版块还未成立,但大家的积极性挺高,不少站长都已经调整好自己的网站,生成了可供Drupal Planet 使用的RSS订阅源. 如果你也想让网站做好准备,可以不必再花上不少 ...