设计模式----观察者模式 实例
2015-06-11 13:34
387 查看
代码:
#include <iostream>
#include <vector>
using namespace std;
class Account;
//Observer抽象类,用作接口
class Observer
{
protected:
Account *account;
public:
Observer(Account *account):account(account) {}
virtual void handle_event()=0;
virtual void set_account_balance(double balance)=0;
virtual ~Observer()
{
cout<<"in the destructor of Observer"<<endl;
}
};
//Account抽象类,用作接口
class Account
{
protected:
vector<Observer*> observer;
double balance;
public:
virtual void add_observer(Observer *o)=0;
virtual void remove_observer(Observer *o)=0;
virtual void notify_observer()=0;
virtual void set_balance(double balance)=0;
virtual double get_balance()=0;
public:
virtual ~Account()
{
cout<<"in the destructor of Account"<<endl;
}
};
//BankAccount具体类,继承了ACcount抽象类
class BankAccount:public Account
{
public:
~BankAccount()
{
cout<<"in the destructor of BankAccount"<<endl;
}
public:
void add_observer(Observer *o)
{
observer.push_back(o);
}
void remove_observer(Observer *o)
{
for(vector<Observer*>::iterator it=observer.begin(); it!=observer.end(); it++)
{
if(o==*it)
{
observer.erase(it);
return;
}
}
}
void notify_observer()
{
for(vector<Observer*>::const_iterator it=observer.begin(); it!=observer.end(); it++)
{
(*it)->handle_event();
}
}
void set_balance(double balance)
{
this->balance=balance;
notify_observer();
} //这个接口很关键
double get_balance()
{
return balance;
}
};
class ATM:public Observer
{
public:
ATM(Account *account):Observer(account)//将acount传递给基类Observer的构造函数
{
}
~ATM()
{
cout<<"in the destructor of ATM"<<endl;
}
public:
void handle_event()
{
cout<<"ATM: the balance of bank accont is changed to "<<account->get_balance()<<endl;
}
void set_account_balance(double balance)
{
account->set_balance(balance);
}
};
class InternetBankService:public Observer
{
public:
InternetBankService(Account *account):Observer(account) {}
~InternetBankService()
{
cout<<"in the destructor InternetBankService "<<endl;
}
public:
void handle_event()
{
cout<<"IBS: the balance of bank account is changed to:"<<account->get_balance()<<endl;
}
void set_account_balance(double balance)
{
account->set_balance(balance);
}
};
int main(int argc, char **argv)
{
Account *account=new BankAccount();
Observer *atm_observer=new ATM(account);
Observer *ibs_observer=new InternetBankService(account);
account->add_observer(atm_observer);
account->add_observer(ibs_observer);
account->set_balance(1000.12);
cout<<"Event is triggered by atm_observer"<<endl;
cout<<"============="<<endl;
atm_observer->set_account_balance(1000.23);
cout<<"Event is triggered by ibs_observer"<<endl;
cout<<"============="<<endl;
ibs_observer->set_account_balance(1000.36);
delete atm_observer;
delete ibs_observer;
return 0;
}
编译、运行结果:
ngnsvr9 [** NONE **]/home/xionghailong/demo/observer $ g++ main.cpp -o main
ngnsvr9 [** NONE **]/home/xionghailong/demo/observer $ ./main
ATM: the balance of bank accont is changed to 1000.12
IBS: the balance of bank account is changed to:1000.12
Event is triggered by atm_observer
=============
ATM: the balance of bank accont is changed to 1000.23
IBS: the balance of bank account is changed to:1000.23
Event is triggered by ibs_observer
=============
ATM: the balance of bank accont is changed to 1000.36
IBS: the balance of bank account is changed to:1000.36
in the destructor of ATM
in the destructor of Observer
in the destructor InternetBankService
in the destructor of Observer
#include <iostream>
#include <vector>
using namespace std;
class Account;
//Observer抽象类,用作接口
class Observer
{
protected:
Account *account;
public:
Observer(Account *account):account(account) {}
virtual void handle_event()=0;
virtual void set_account_balance(double balance)=0;
virtual ~Observer()
{
cout<<"in the destructor of Observer"<<endl;
}
};
//Account抽象类,用作接口
class Account
{
protected:
vector<Observer*> observer;
double balance;
public:
virtual void add_observer(Observer *o)=0;
virtual void remove_observer(Observer *o)=0;
virtual void notify_observer()=0;
virtual void set_balance(double balance)=0;
virtual double get_balance()=0;
public:
virtual ~Account()
{
cout<<"in the destructor of Account"<<endl;
}
};
//BankAccount具体类,继承了ACcount抽象类
class BankAccount:public Account
{
public:
~BankAccount()
{
cout<<"in the destructor of BankAccount"<<endl;
}
public:
void add_observer(Observer *o)
{
observer.push_back(o);
}
void remove_observer(Observer *o)
{
for(vector<Observer*>::iterator it=observer.begin(); it!=observer.end(); it++)
{
if(o==*it)
{
observer.erase(it);
return;
}
}
}
void notify_observer()
{
for(vector<Observer*>::const_iterator it=observer.begin(); it!=observer.end(); it++)
{
(*it)->handle_event();
}
}
void set_balance(double balance)
{
this->balance=balance;
notify_observer();
} //这个接口很关键
double get_balance()
{
return balance;
}
};
class ATM:public Observer
{
public:
ATM(Account *account):Observer(account)//将acount传递给基类Observer的构造函数
{
}
~ATM()
{
cout<<"in the destructor of ATM"<<endl;
}
public:
void handle_event()
{
cout<<"ATM: the balance of bank accont is changed to "<<account->get_balance()<<endl;
}
void set_account_balance(double balance)
{
account->set_balance(balance);
}
};
class InternetBankService:public Observer
{
public:
InternetBankService(Account *account):Observer(account) {}
~InternetBankService()
{
cout<<"in the destructor InternetBankService "<<endl;
}
public:
void handle_event()
{
cout<<"IBS: the balance of bank account is changed to:"<<account->get_balance()<<endl;
}
void set_account_balance(double balance)
{
account->set_balance(balance);
}
};
int main(int argc, char **argv)
{
Account *account=new BankAccount();
Observer *atm_observer=new ATM(account);
Observer *ibs_observer=new InternetBankService(account);
account->add_observer(atm_observer);
account->add_observer(ibs_observer);
account->set_balance(1000.12);
cout<<"Event is triggered by atm_observer"<<endl;
cout<<"============="<<endl;
atm_observer->set_account_balance(1000.23);
cout<<"Event is triggered by ibs_observer"<<endl;
cout<<"============="<<endl;
ibs_observer->set_account_balance(1000.36);
delete atm_observer;
delete ibs_observer;
return 0;
}
编译、运行结果:
ngnsvr9 [** NONE **]/home/xionghailong/demo/observer $ g++ main.cpp -o main
ngnsvr9 [** NONE **]/home/xionghailong/demo/observer $ ./main
ATM: the balance of bank accont is changed to 1000.12
IBS: the balance of bank account is changed to:1000.12
Event is triggered by atm_observer
=============
ATM: the balance of bank accont is changed to 1000.23
IBS: the balance of bank account is changed to:1000.23
Event is triggered by ibs_observer
=============
ATM: the balance of bank accont is changed to 1000.36
IBS: the balance of bank account is changed to:1000.36
in the destructor of ATM
in the destructor of Observer
in the destructor InternetBankService
in the destructor of Observer
相关文章推荐
- 寒门再难出贵子
- jq 地区(省市县区)联动菜单
- UIImagePickerController 的基本用法 - iOS - UI基础知识总结14
- ural1006 Square Frames
- Python sys.argv的用法
- UFLDL教程笔记及练习答案四(建立分类用深度学习---栈式自编码神经网络)
- 游标-遍历-查询-1
- 注解cascade = CascadeType.PERSIST 与cascade = CascadeType.all
- Ewebeditor最新漏洞和漏洞指数
- 阿里云CentOS中vsftp安装、配置、卸载
- 异步处理Servlet接收到的请求
- 设置-安全-手机加密功能讲解
- find . -mtime +1 查找文件
- 获取时间戳的方法
- AE特效-与MAYA的结合、制作音乐舞蹈太极动作
- repo 命令 切换源码工程分支
- Mysql 拿指定经纬度与数据库多条经纬度进行距离计算
- 大球与小球
- Firebug: Net Panel 使用详解
- 24式太极拳:3D动画演示(图文)