c++实现状态模式
2021/11/18 12:39:40
本文主要是介绍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; }View Code1
#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; }View Code2
两种方法运行结果:
个人觉得第一种比较好。
这篇关于c++实现状态模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享