#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. 利用mysql-proxy 代理无法迁移数据库

    一.什么是数据库迁移? 随着业务的增长或机器老化等原因,不可避免会碰到将数据库从一台机器迁移到另一台机器(集群)的问题.数据库迁移可分为冷迁(离线)和热迁(在线实时). 二.如何无缝迁移? 以旧库 1 ...

  2. Spring切入点表达式常用写法

    自从使用AspectJ风格切面配置,使得Spring的切面配置大大简化,但是AspectJ是另外一个开源项目,其规则表达式的语法也稍稍有些怪异.   下面给出一些常见示例的写法:   比如,下面是一个 ...

  3. Win7允许被ping

    我们可以通过命令行方式来执行入站 Ping 的规则是启用还是禁用,命令行如下: netsh firewall set icmpsetting 8 netsh firewall set icmpsett ...

  4. $.ajax()引发的对Deferred的总结

    传统的ajax写法: $.ajax({ url:"1.json", type:"get", success:function(data){}, error:fu ...

  5. 最简的Dubbo例子部署

    dubbo 中包含下面4个核心组件: 生产者.消费者.注册中心.监控中心.   简单部署的模块关系 生产者.消费者 最简版本的Dubbo部署只运行Demo Provider和Demo Consumer ...

  6. SCI期刊的审稿流程

    审稿中涉及到的人: EIC-Editor in Chief 主编, 此人很重要,有稿件最终决定权. ADM- (可能是) Administrator 应该是协助主编日常工作的. AE-Associat ...

  7. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  8. JDBC数据更新

    在JDBC中通常用Statement类的对象实现对数据库的更新(增.删.查.改)操作 //1.获取数据库连接 connection = getConnection(); //2.准备sql语句 Str ...

  9. Redis附加功能之Redis事务

    一.事务 Redis 的事务功能允许用户将多个命令包裹起来,然后一次性地.按顺序地执行被包裹的所有命令.在事务执行的过程中,服务器不会中断事务而改去执行其他命令请求,只有在事务包裹的所有命令都被执行完 ...

  10. 操作系统是怎么工作的——mykernel环境的搭建

    可以参见:https://github.com/mengning/mykernel 首先感谢:http://www.euryugasaki.com/archives/1014 1.搭建实验环境(实验环 ...