一、实验目的及要求
1、掌握行为型模式的概念。
2、掌握备忘录模式、观察者模式、状态模式、策略模式、模板模式、访问者模式的构造方式及使用情景。
二、实验设备(环境)
1、 软件需求: Dev-Cpp5.4, Rational Rose / Microsoft Visio
2、 硬件需求: Pentium III 450以上的CPU处理器,1G以上的内存,2G的自由硬盘空间
三、实验内容
1、在某银行系统定义的账户有三种状态:
(1) 如果账户(Account)中余额(balance)大于等于0,此时账户的状态为绿色(GreenState),即正常状态,表示既可以向该账户存款(deposit)也可以从该账户取款(withdraw);
(2) 如果账户中余额小于0,并且大于等于-1000,则账户的状态为黄色(YellowState),即欠费状态,此时既可以向该账户存款也可以从该账户取款;
(3) 如果账户中余额小于-1000,那么账户的状态为红色(RedState),即透支状态,此时用户只能向该账户存款,不能再从中取款。
现用状态模式来实现状态的转化问题,用户只需要执行简单的存款和取款操作,系统根据余额数量自动转换到相应的状态。银行账户实例类图和状态图分别如图1和图2。
2、对于题目1中的银行系统账户,使用观察者模式和策略模式作改进,使得:
(1)在图1中增加银行类Bank,当银行中有新的银行政策出台或新的金融产品发布,就向所有的账户Account发短信消息notify(string message)。
(2)对于图1中的账户类Account,关联一个消息策略类MessageStrategy,增加相应的成员变量msStrategy和成员函数setStrategy(),可以更换账户接收消息的方式,并在成员函数Response(string message)中根据不同的策略(如微信或短信)给账户提示消息:如果是银行类Bank发送的通知,则在MessageBox窗口(模拟短信)中显示消息;如果是账户变动产生的提醒,则在控制台(模拟微信)中打印余额Balance和账户状态消息。
图1 银行账户实例类图
图2 银行账户状态图
四、实验步骤与结果
练习一
1.状态模式设计结构图UML图:
2.实验结果截图:
3.代码分析:
Account.h
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include
#include
#include
using namespace std;
class AccountState;
class Account
{
public:
Account(string owner,double init);
~Account();
void deposit(double amount);
void withdraw(double amount);
void setState(AccountState* state);
private:
string owner;
AccountState* state;
};
#endif //_ACCOUNT_H_
Account.cpp
#include "Account.h"
#include "AccountState.h"
#include
#include
#include
#include
#include
using namespace std;
Account::Account(string owner,double init)
{
this->owner = owner;
this->state = new GreenState(init, this);
cout<<"账户:"<owner<<"初始金额:"<state = state;
}
void Account::deposit(double amount)
{
state->deposit(amount);
double a=state->balance;
stringstream s;
s<>s1;
string s2=state->getName();
string s3=this->owner;
cout<<" 现金余额为:"<withdraw(amount);
double a=state->balance;
stringstream s;
s<>s1;
string s2=state->getName();
string s3=this->owner;
cout<<" 现金余额为:"<
AccountState.h
#ifndef _ACCOUNTSTATE_H_
#define _ACCOUNTSTATE_H_
#include
using namespace std;
class Account;
class AccountState
{
public:
virtual ~AccountState();
AccountState();
virtual void deposit(double amount)=0;
virtual void withdraw(double amount)=0;
virtual void stateCheck()=0;
virtual string getName();
double balance;
Account* acc;
};
class YellowState:public AccountState
{
public:
YellowState(AccountState* acc);
~YellowState();
void deposit(double amount);
void withdraw(double amount);
void stateCheck();
string getName();
};
class GreenState:public AccountState
{
public:
GreenState(AccountState* acc);
GreenState(double balance,Account* acc);
~GreenState();
void deposit(double amount);
void withdraw(double amount);
void stateCheck();
string getName();
};
class RedState:public AccountState
{
public:
RedState(AccountState* acc);
~RedState();
void deposit(double amount);
void withdraw(double amount);
void stateCheck();
string getName();
};
#endif //_ACCOUNTSTATE_H_
AccountState.cpp
#include "Account.h"
#include "AccountState.h"
#include
using namespace std;
AccountState::AccountState()
{
}
AccountState::~AccountState()
{
}
void AccountState::withdraw(double amount)
{
}
void AccountState::deposit(double amount)
{
}
void AccountState::stateCheck()
{
}
string AccountState::getName()
{
return " ";
}
YellowState::YellowState(AccountState* state)
{
this->balance =state->balance;
this->acc = state->acc;
}
YellowState::~YellowState()
{
}
void YellowState::stateCheck() {
if(balance>=0){
acc->setState(new GreenState(this));
}else if(balance<-1000){
acc->setState(new RedState(this));
}
}
void YellowState::deposit(double amount)
{
this->balance+=amount;
stateCheck();
}
void YellowState::withdraw(double amount)
{
this->balance-=amount;
stateCheck();
}
string YellowState::getName()
{
return "YellowState";
}
GreenState::GreenState(AccountState* state)
{
this->balance =state->balance;
this->acc = state->acc;
}
GreenState::GreenState(double balance,Account* acc)
{
this->balance = balance;
this->acc = acc;
}
GreenState::~GreenState()
{
}
void GreenState::stateCheck() {
if(balance>=-1000&&balance<0){
acc->setState(new YellowState(this));
}else if(balance<-1000){
acc->setState(new RedState(this));
}
}
void GreenState::deposit(double amount)
{
this->balance+=amount;
stateCheck();
}
void GreenState::withdraw(double amount)
{
this->balance-=amount;
stateCheck();
}
string GreenState::getName()
{
return "GreenState";
}
RedState::RedState(AccountState* state)
{
this->balance =state->balance;
this->acc = state->acc;
}
RedState::~RedState()
{
}
void RedState::stateCheck() {
if(balance>=0){
acc->setState(new GreenState(this));
}else if(balance>=-1000){
acc->setState(new YellowState(this));
}
}
void RedState::deposit(double amount)
{
this->balance+=amount;
stateCheck();
}
void RedState::withdraw(double amount)
{
cout<<"透支状态,无法取款"<
main.cpp
#include "Account.h"
#include "AccountState.h"
#include
#include
using namespace std;
int main(int argc,char* argv[])
{
Account* acc = new Account("wubifen",100);
AccountState*state=new GreenState(0,acc);
acc->deposit(1000);
acc->deposit(600);
acc->deposit(200);
acc->withdraw(2000);
acc->withdraw(500);
acc->withdraw(700);
delete acc;
return 0;
}
练习二
1.观察者模式设计结构图UML图:
2.实验结果截图:
3.代码分析:
Account.h
#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include
#include
#include
using namespace std;
class Bank;
class AccountState;
class MessageStrategy;
class Account
{
public:
Account(string owner,double init);
Account(Bank*bank):bank(bank){}
~Account();
void deposit(double amount);
void withdraw(double amount);
void setState(AccountState* state);
void Response(string message);
void setStrategy(MessageStrategy* msStrategy);
void Notify(string message);
private:
string owner;
AccountState* state;
MessageStrategy* msStrategy;
Bank *bank ;
};
#endif //_ACCOUNT_H_
Account.cpp
#include "Account.h"
#include "AccountState.h"
#include"Bank.h"
#include"MessageStrategy.h"
#include
#include
#include
#include
#include
using namespace std;
Account::Account(string owner,double init)
{
this->owner = owner;
this->state = new GreenState(init, this);
cout<<"账户:"<owner<<"初始金额:"<state = state;
}
void Account::setStrategy(MessageStrategy* msStrategy)
{
this->msStrategy=msStrategy;
}
void Account::Response(string message)
{
msStrategy->AlgrithmInterface(message);
}
void Account::Notify(string message)
{
MessageStrategy* m1=new MessageStrategy1();
this->setStrategy(m1);
Response(message);
}
void Account::deposit(double amount)
{
state->deposit(amount);
double a=state->balance;
MessageStrategy* m1=new MessageStrategy2();
this->setStrategy(m1);
stringstream s;
s<>s1;
string s2=state->getName();
string s3=this->owner;
string message1="微信交易提醒:尊敬的邬必芬,您的借记卡发生了一笔动账交易:"+s3+" 现金余额为:"+s1+" 账户状态为:"+s2;
this->Response(message1);
}
void Account::withdraw(double amount)
{
state->withdraw(amount);
double a=state->balance;
MessageStrategy* m2=new MessageStrategy2();
this->setStrategy(m2);
stringstream s;
s<>s1;
string s2=state->getName();
string s3=this->owner;
string message2="微信交易提醒:尊敬的邬必芬,您的借记卡发生了一笔动账交易"+s3+" 现金余额为:"+s1+" 账户状态为:"+s2;
this->Response(message2);
}
AccountState.h
#ifndef _ACCOUNTSTATE_H_
#define _ACCOUNTSTATE_H_
#include
using namespace std;
class Account;
class AccountState
{
public:
virtual ~AccountState();
AccountState();
virtual void deposit(double amount)=0;
virtual void withdraw(double amount)=0;
virtual void stateCheck()=0;
virtual string getName();
double balance;
Account* acc;
};
class YellowState:public AccountState
{
public:
YellowState(AccountState* acc);
~YellowState();
void deposit(double amount);
void withdraw(double amount);
void stateCheck();
string getName();
};
class GreenState:public AccountState
{
public:
GreenState(AccountState* acc);
GreenState(double balance,Account* acc);
~GreenState();
void deposit(double amount);
void withdraw(double amount);
void stateCheck();
string getName();
};
class RedState:public AccountState
{
public:
RedState(AccountState* acc);
~RedState();
void deposit(double amount);
void withdraw(double amount);
void stateCheck();
string getName();
};
#endif //_ACCOUNTSTATE_H_
AccountState.cpp
#include "Account.h"
#include "AccountState.h"
#include
using namespace std;
AccountState::AccountState()
{
}
AccountState::~AccountState()
{
}
void AccountState::withdraw(double amount)
{
}
void AccountState::deposit(double amount)
{
}
void AccountState::stateCheck()
{
}
string AccountState::getName()
{
return " ";
}
YellowState::YellowState(AccountState* state)
{
this->balance =state->balance;
this->acc = state->acc;
}
YellowState::~YellowState()
{
}
void YellowState::stateCheck() {
if(balance>=0){
acc->setState(new GreenState(this));
}else if(balance<-1000){
acc->setState(new RedState(this));
}
}
void YellowState::deposit(double amount)
{
this->balance+=amount;
stateCheck();
}
void YellowState::withdraw(double amount)
{
this->balance-=amount;
stateCheck();
}
string YellowState::getName()
{
return "YellowState";
}
GreenState::GreenState(AccountState* state)
{
this->balance =state->balance;
this->acc = state->acc;
}
GreenState::GreenState(double balance,Account* acc)
{
this->balance = balance;
this->acc = acc;
}
GreenState::~GreenState()
{
}
void GreenState::stateCheck() {
if(balance>=-1000&&balance<0){
acc->setState(new YellowState(this));
}else if(balance<-1000){
acc->setState(new RedState(this));
}
}
void GreenState::deposit(double amount)
{
this->balance+=amount;
stateCheck();
}
void GreenState::withdraw(double amount)
{
this->balance-=amount;
stateCheck();
}
string GreenState::getName()
{
return "GreenState";
}
RedState::RedState(AccountState* state)
{
this->balance =state->balance;
this->acc = state->acc;
}
RedState::~RedState()
{
}
void RedState::stateCheck() {
if(balance>=0){
acc->setState(new GreenState(this));
}else if(balance>=-1000){
acc->setState(new YellowState(this));
}
}
void RedState::deposit(double amount)
{
this->balance+=amount;
stateCheck();
}
void RedState::withdraw(double amount)
{
cout<<"透支状态,无法取款"<
Bank.h
#ifndef _BANK_H_
#define _BANK_H_
#include
#include
using namespace std;
class Account;
class Bank
{
public:
Bank();
~Bank();
void notify(string message);
listaccs;
void Attach(Account *acc);
void Detach(Account *acc);
};
#endif //_BANK_H_
Bank.cpp
#include"Account.h"
#include"AccountState.h"
#include"Bank.h"
#include
#include
using namespace std;
class Account;
Bank::Bank()
{
}
Bank::~Bank()
{
}
void Bank::notify(string message){
list::iterator i=accs.begin();
while(i!=accs.end())
{
(*i)->Notify(message);
++i;
}
}
void Bank::Attach(Account* acc){
accs.push_back(acc);
}
void Bank::Detach(Account* acc){
accs.remove(acc);
}
MessageStrategy.h
#ifndef _MESSAGESTRATEGY_H_
#define _MESSAGESTRATEGY_H_
#include
#include
using namespace std;
class MessageStrategy
{
public:
MessageStrategy();
virtual ~MessageStrategy();
virtual void AlgrithmInterface(string message)=0;
protected:
private:
};
class MessageStrategy1:public MessageStrategy
{
public:
MessageStrategy1();
virtual ~MessageStrategy1();
void AlgrithmInterface(string message);
protected:
private:
};
class MessageStrategy2:public MessageStrategy
{
public:
MessageStrategy2();
virtual ~MessageStrategy2();
void AlgrithmInterface(string message);
protected:
private:
};
#endif //_MESSAGESTRATEGY_H_
MessageStrategy.cpp
#include "MessageStrategy.h"
#include
#include
#include
#include
using namespace std;
MessageStrategy::MessageStrategy()
{
}
MessageStrategy::~MessageStrategy()
{
}
void MessageStrategy::AlgrithmInterface(string message)
{
}
MessageStrategy1::MessageStrategy1()
{
}
MessageStrategy1::~MessageStrategy1()
{
}
void MessageStrategy1::AlgrithmInterface(string message)
{
MessageBox( NULL , TEXT(message.c_str()) ,
TEXT("短信提醒") , MB_ICONINFORMATION|MB_YESNO);
}
MessageStrategy2::MessageStrategy2()
{
}
MessageStrategy2::~MessageStrategy2()
{
}
void MessageStrategy2::AlgrithmInterface(string message)
{
cout<
main.cpp
#include "Account.h"
#include "AccountState.h"
#include"Bank.h"
#include
#include
using namespace std;
int main(int argc,char* argv[])
{
Account* acc = new Account("邬必芬",100);
AccountState*state=new GreenState(0,acc);
Bank*bank = new Bank();
bank->Attach(acc);
string s="窗口提示,来着银行账户的短信提醒,尊敬的邬必芬,您正在使用借记卡";
bank->notify(s);
acc->deposit(1000);
acc->deposit(600);
acc->deposit(200);
acc->withdraw(2000);
acc->withdraw(500);
acc->withdraw(700);
delete bank;
delete acc;
return 0;
}