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设计模式]山西面馆中的设计模式— ...
随机推荐
- linux tr命令实现windows文本格式与linux文本格式间的转换
tr 命令 转换和删除字符 选项: -d --delete:删除字符 -s --squeeze-repeats:把连续重复的字符以一个字符表示,即去重 -c –C --complement:取字符集的 ...
- tip多文件上传(自找)
HTMl代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- 判断jQuery是否加载,如果未加载则加载
一般情况返回的js这么写history.go(-1) 但是如果是从别人分享过来的,就没有反应 所以就做个判断,没有上一页就返回首页 if (typeof jQuery == 'undefined') ...
- 软件工程homework-001
一. 回顾你过去将近3年的学习经历 1.当初你报考的时候,是真正喜欢计算机这个专业吗? 答:不喜欢,高中时候就已经对计算机比较抵触了,家里小时候比较富裕,九几年就在日本买了第一批家用台式机 ...
- 堆优化Dijkstra算法
但是,我们会发现刚刚讲的朴素Dijkstra算法(高情商:朴素 : 低情商: 低效)的套路不适用于稀疏图,很容易会爆时间: 所以,我们要对其中的一些操作进行优化,首先我们发现找到里起始点最近的点去更新 ...
- 闲聊系列之 5-why root cause分析法
本篇参考: https://max.book118.com/html/2017/1126/141669829.shtm https://baike.baidu.com/item/5why%E5%88% ...
- github:git clone下载加速以及vim-plug下载插件加速
git clone 下载加速 1. 先在github将仓库地址复制下来 2. git clone时将https://github.com/* 改为https://gitclone.com/github ...
- Centos7下开启防火墙,允许通过的端口
1.查看防火墙状态 systemctl status firewalld 2.如果不是显示active状态,需要打开防火墙 systemctl start firewalld 3.# 查看所有已开放的 ...
- 6月5日 python学习总结 jQuery (二)
1. 操作样式 对CSS类的操作: addClass();// 添加指定的CSS类名. removeClass();// 移除指定的CSS类名. hasClass();// 判断样式存不存在 ...
- python发送邮件之宝藏库yagmail
之前使用email模块+smtplib模块发送邮件,虽然可以实现功能,但过程比较繁琐,今天发现一个宝藏库(yagmail),可以说是炒鸡好用啦!!! 贴上实现代码,大家细品 yagmail安装 pip ...