c++实现状态模式
实验:用Java代码模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
由于是c++,不像java那么灵活,所以类的调用方面出了些许多问题,包括调用,出现了很多错误,不过好在都解决了。

代码:

#include<iostream>
using namespace std;
class Account;
class AccountState{
public:
Account *acc;
double balance;
string stateName;
public:
virtual void stateCheck()=0;
void deposit(double amount);
virtual void withdraw(double amount);
};
class Account{
private:
AccountState *state;
string owner;
public:
Account(string owner,double init); void setState(AccountState *state) {
this->state=state;
}
AccountState* getState() {
return this->state;
}
string getOwner() {
return this->owner;
}
void deposit(double amount) {
state->deposit(amount);
}
void withdraw(double amount) {
state->withdraw(amount);
}
};
class RedState :public AccountState{
public:
RedState(AccountState *state) {
this->balance = state->balance;
this->acc = state->acc;
this->stateName="透支状态";
}
void withdraw(double amount){cout<<"您的账户处于透支状态,不能取款!"<<endl;}
void stateCheck();
};
class YellowState :public AccountState{
public:
YellowState(AccountState *state) {
this->balance = state->balance;
this->acc = state->acc;
this->stateName="欠费状态";
}
void stateCheck();
};
class GreenState:public AccountState{
public:
GreenState(double balance,Account *acc) {
this->balance = balance;
this->acc = acc;
this->stateName="正常状态";
}
GreenState(AccountState *state) {
this->acc=state->acc;
this->balance=state->balance;
this->stateName="正常状态";
}
void stateCheck() {
if(balance>=-1000&&balance<0) {
acc->setState(new YellowState(this));
}else if(balance<-1000) {
acc->setState(new RedState(this));
}
else{
acc->setState(new GreenState(this));
}
}
};
void RedState::stateCheck(){
if(balance>=-1000&&balance<0) {
acc->setState(new YellowState(this));
}else if(balance<-1000) {
acc->setState(new RedState(this));
}
else {
acc->setState(new GreenState(this));
}
}
void YellowState::stateCheck() {
if(balance>=-1000&&balance<0) {
acc->setState(new YellowState(this));
}else if(balance<-1000) {
acc->setState(new RedState(this));
}
else{
acc->setState(new GreenState(this));
}
}
void AccountState::deposit(double amount){
cout<<acc->getOwner()<<"存款"<<amount<<endl;
this->balance+=amount;
stateCheck();
cout<<"账户余额:"<<this->balance<<endl;
cout<<"现在账户状态:"<<acc->getState()->stateName<<endl;
}
void AccountState::withdraw(double amount){
cout<<acc->getOwner()<<"取款"<<amount<<endl;
this->balance-=amount;
stateCheck();
cout<<"账户余额:"<<this->balance<<endl;
cout<<"现在账户状态:"<<acc->getState()->stateName<<endl;
}
Account::Account(string owner,double init){
this->owner=owner;
this->state=new GreenState(init,this);
cout<<"恭喜"<<this->owner<<"开户成功!银行卡初始金额:"<<init<<endl;
cout<<"------------------------------"<<endl;
}
int main(){
Account *account=new Account("张三",100);
account->deposit(100);
cout<<"------------------------------"<<endl;
account->withdraw(500);
cout<<"------------------------------"<<endl;
account->deposit(1000);
cout<<"------------------------------"<<endl;
account->withdraw(2000);
cout<<"------------------------------"<<endl;
cout<<account->getState()->stateName;
account->withdraw(100);
cout<<"------------------------------"<<endl;
account->deposit(2000);
cout<<"------------------------------"<<endl;
return 0;
}
1

#include<iostream>
using namespace std;
class Account;
class AccountState{
public:
Account *acc;
double balance;
string stateName;
public:
virtual void stateCheck()=0;
virtual void deposit(double amount)=0;
virtual void withdraw(double amount)=0;
};
class Account{
private:
AccountState *state;
string owner;
public:
Account(string owner,double init); void setState(AccountState *state) {
this->state=state;
}
AccountState* getState() {
return this->state;
}
string getOwner() {
return this->owner;
}
void checkState(){
state->stateCheck();
}
void deposit(double amount) {
cout<<this->owner<<"存款"<<amount<<endl;
state->deposit(amount);
cout<<"账户余额:"<<state->balance<<endl;
cout<<"现在账户状态:"<<state->stateName<<endl;
}
void withdraw(double amount) {
cout<<this->owner<<"取款"<<amount<<endl;
state->withdraw(amount);
cout<<"账户余额"<<state->balance<<endl;
cout<<"现在账户状态:"<<state->stateName<<endl;
}
};
class RedState :public AccountState{
public:
RedState(AccountState *state) {
this->balance = state->balance;
this->acc = state->acc;
this->stateName="透支状态";
}
void stateCheck();
void deposit(double amount) {
this->balance+=amount;
stateCheck();
}
void withdraw(double amount) {
cout<<"您的账户处于透支状态,不能取款!"<<endl;
}
};
class YellowState :public AccountState{
public:
YellowState(AccountState *state) {
this->balance = state->balance;
this->acc = state->acc;
this->stateName="欠费状态";
}
void stateCheck();
void deposit(double amount) {
this->balance+=amount;
stateCheck();
}
void withdraw(double amount) {
this->balance-=amount;
stateCheck();
}
};
class GreenState:public AccountState{
public:
GreenState(double balance,Account *acc) {
this->balance = balance;
this->acc = acc;
this->stateName="正常状态";
}
GreenState(AccountState *state) {
this->acc=state->acc;
this->balance=state->balance;
this->stateName="正常状态";
}
void stateCheck() {
if(balance>=-1000&&balance<0) {
acc->setState(new YellowState(this));
}else if(balance<-1000) {
acc->setState(new RedState(this));
}
else{
acc->setState(new GreenState(this));
}
}
void deposit(double amount) {
this->balance+=amount;
stateCheck();
}
void withdraw(double amount) {
this->balance-=amount;
stateCheck();
}
};
void RedState::stateCheck(){
if(balance>=-1000&&balance<0) {
acc->setState(new YellowState(this));
}else if(balance<-1000) {
acc->setState(new RedState(this));
}
else {
acc->setState(new GreenState(this));
}
}
void YellowState::stateCheck() {
if(balance>=-1000&&balance<0) {
acc->setState(new YellowState(this));
}else if(balance<-1000) {
acc->setState(new RedState(this));
}
else{
acc->setState(new GreenState(this));
}
}
Account::Account(string owner,double init){
this->owner=owner;
this->state=new GreenState(init,this);
cout<<"恭喜"<<this->owner<<"开户成功!银行卡初始金额:"<<init<<endl;
cout<<"------------------------------"<<endl;
}
int main(){
Account *account=new Account("张三",100);
account->deposit(100);
cout<<"------------------------------"<<endl;
account->withdraw(500);
cout<<"------------------------------"<<endl;
account->deposit(1000);
cout<<"------------------------------"<<endl;
account->withdraw(2000);
cout<<"------------------------------"<<endl;
account->withdraw(100);
cout<<"------------------------------"<<endl;
account->deposit(2000);
cout<<"------------------------------"<<endl;
return 0;
}
2
两种方法运行结果:


个人觉得第一种比较好。
c++实现状态模式的更多相关文章
- StatePattern(状态模式)
/** * 状态模式 * @author TMAC-J * 状态模式和策略模式很像,其实仔细研究发现完全不一样 * 策略模式各策略之间没有任何关系,独立的 * 状态模式各状态之间接口方法都是一样的 * ...
- 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)
说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...
- php实现设计模式之 状态模式
<?php /*状态模式:允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类.(行为模式) * * 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做 ...
- Java 策略模式和状态模式
本文是转载的,转载地址:大白话解释Strategy模式和State模式的区别 先上图: 本质上讲,策略模式和状态模式做得是同一件事:去耦合.怎么去耦合?就是把干什么(语境类)和怎么干(策略接口)分开, ...
- javascript - 状态模式 - 简化分支判断流程
状态模式笔记 当一个对象的内部状态发生改变时,会导致行为的改变,这像是改变了对象 状态模式既是解决程序中臃肿的分支判断语句问题,将每个分支转化为一种状态独立出来,方便每种状态的管理又不至于每次 ...
- C#设计模式系列:状态模式(State)
1.状态模式简介 1.1>.定义 状态模式的核心思想是允许一个对象在它的内部状态改变时改变它的行为,即不同的状态对应不同的行为. 状态模式的针对性很强,当有状态变化的时候可以选择状态模式. 1. ...
- 十一个行为模式之状态模式(State Pattern)
定义: 当一个对象有多个状态,并且在每个状态下有不同的行为,可以使用状态模式来在其内部改变状态时改变其行为,而客户端不会察觉状态的改变,仍使用同样的方法或接口与对象进行交互. 结构图: Context ...
- java设计模式之状态模式
状态模式 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类. 状态模式UML图 上下文环境(Context):它定义了客户程序需要的接口并维护一个具体状态角色的实例,将与状态相关 ...
- iOS - 在工程中试玩状态模式
做了一个项目,项目中一个藏品详情界面针对不同用户,和用户所处于的状态的不同,展示的效果和操作的权限都会不同.想到了状态模式,从来没有用过,赶紧学一下然后用一用.期待兴奋 看了这么多的博客,终于找到一个 ...
- [Head First设计模式]生活中学设计模式——状态模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
随机推荐
- 创建 maven项目时:Cannot resolve Plugin org.apache.maven.plugins:maven-install-plugin报错
Maven在每一次下载jar包的过程中,一旦第一次下载完成后,就会有一个lastUpdate文件,表示该jar包已经下载.下次再检索这个包,也就不会去远程仓库进行下载. 解决办法:找到自己的maven ...
- react 也就这么回事 05 —— 组件 & Props
什么是组件:用来实现局部功能的可复用代码片段 比如很多界面会用到"分页"功能,因此可以将它封装成独立的组件 这样用到分页的界面只需引入该组件而不必重新写代码 1 定义组件 在 Re ...
- LGP3307题解
题意有点儿神秘,而且出题人可能有点大病( 项链由 \(n\) 颗珠子构成,相邻的珠子不能相同. 每颗珠子上有 \(3\) 个数字,这 \(3\) 个数之间没有顺序,且 \(\gcd\) 为 \(1\) ...
- CF594D题解
我不会数据结构/kk 我想题意应该十分清楚了. 我们知道 \(\varphi(p^k)=p^{k-1}(p-1)\),那么我们考虑将一个询问下放到右端点,然后往右移动右端点并更新每个左端点到右端点的答 ...
- LGP2414题解
难不成是我后缀自动机学魔怔了,AC 自动机都能套上线段树 题意:给你一颗 Trie,每次询问两个节点 \(u,v\),\(u\) 代表的字符串在 \(v\) 代表的字符串中出现了多少次. 让我们思考一 ...
- Windows XP系统搜索故障及处理办法点点通
故障1:单击资源管理器工具栏上的"搜索"按钮或者按F3,系统无任何响应. 解决方法:首先进入C:\Windows\inf文件夹(该文件夹属性为隐藏),右键单击srchasst.in ...
- Citus 分布式 PostgreSQL 集群 - SQL Reference(创建和修改分布式表 DDL)
创建和分布表 要创建分布式表,您需要首先定义表 schema. 为此,您可以使用 CREATE TABLE 语句定义一个表,就像使用常规 PostgreSQL 表一样. CREATE TABLE ht ...
- Linux C申请内存三种基本方式
一份代码可以知道具体方式和原理: int main() { int stack_a; int stack_b; static int static_c; static int static_d; in ...
- KVM虚拟机cpu资源限制和vcpu亲缘性绑定
前言 KVM中添加的实例存在资源分布不均的情况,这样如果有消耗资源的实例会影响到其他实例的服务正常运行,所以给kvm做资源限制是很有必要的,下面记录一下在centos7中KVM环境下使用cgroup限 ...
- Mysql常用操作笔记
目录 登录 退出 Sql语句分类 DDL操作数据库 1.创建数据库 2.查看数据库 3.修改数据库 4.删除数据库 5.使用数据库 6.创建表 7.查看表 8.删除表 9.修改表 10.常用字段类型 ...