转载自:
http://patmusing.blog.163.com/blog/static/13583496020101501923571/
也称为Dependents或Publish-Subscribe模式。
定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动更新。
“Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.” – GoF
将一个系统分解成一系列的相互协作的类会产生一个普遍存在的副作用:需要维护相关对象之间的一致性。要维护各对象之间的一致性,通常的做法就是使这些对象之间彼此紧耦合,但是这样又会降低他们的可重用性。
比如,许多图形界面工具包将用户界面的展示和底层应用数据分开。用来定义应用数据和界面展示的类可以独立重复使用,也可以一起使用。表格对象和柱状图对象可以同时展示同一个应用数据对象中的数据,但表格对象和柱状图对象,彼此无需知道对方的存在。但实际情况中,它们却似乎又知道对方的存在,比如当一个用户改动表格中的信息,柱状图也会随之改变,反之亦然。
这种行为暗示了表格对象和柱状图对象都依赖了数据对象。如果一个用户改变了表格中的数据,这种改动会立即被反映到数据对象中,数据对象中的数据发生了这种改变,会立即通知柱状图对象,柱状图对象得到通知后,会对柱状图的显示做出相应的改变。
Observer模式描述如何建立这些关系。本模式中的关键对象是Subject和Observer。一个Subject可以有任意多独立的Observer。如果Subject的内容发生改变,所有的Observer都会得到通知,每个Observer则查询Subject,以保持与其状态同步。
这种交互也称为publish-subscribe(发布者-注册)。Subject就是publisher,任意数量的Observer可以注册以接收通知。
UML类图:
业务案例:
说明:
- 通过ATM、InternetBankingService和MobileBankingService均可改动银行账户余额;
- 银行账户余额还可以有其他方式改变,比如柜台(此例并未用及,用来模拟银行账户自身的余额变化);
- 只要一上其中之一改变了银行账户中的余额,其他相关各方均可以收到余额变动信息;
- Bank Account是Subject,ATM、InternetBankingService和MobileBankingService是Observer。
C++实现上述业务案例的代码如下:
// Observer.h
#include <iostream>
#include <vector>
using namespace std;
class Observer;
//Account抽象类,用做接口
class Account
{
protected:
vector<Observer *> observers; // 保存若干个Observer
double balance; // 账户余额
public:
virtual void add_observer(Observer *o) = 0; // 注册一个Observer
virtual void remove_observer(Observer *o) = 0; // 删除一个Observer
virtual void notify_observer() = 0; // 通知所有注册了的Observer
virtual void set_balance(double balance) = 0; // 改动账户余额
virtual double get_balance() = 0; // 获取账户月娥
public:
virtual ~Account()
{
cout << "in the destructor of Account..." << endl;
}
};
// 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;
}
};
// BankAccout具体类,继承Account抽象类
class BankAccount : public Account
{
public:
~BankAccount()
{
cout << "in the destructor of BankAccount..." << endl;
}
public:
void add_observer(Observer *o)
{
observers.push_back(o);
}
void remove_observer(Observer *o)
{
for(vector<Observer *>::iterator it = observers.begin(); it != observers.end(); it++)
{
if(o == *it) // 注意*it就是observers中的元素,其类型为Observer *
{
observers.erase(it); // 这里需要注意,o从observers中被删除,但并未从内存中删除
return;
}
}
}
void notify_observer() // 向observers中的所有Observer发出通知,并进行相关处理
{
for(vector<Observer *>::const_iterator it = observers.begin(); it != observers.end(); it++)
{
(*it)->handle_event();
}
}
void set_balance(double balance)
{
this->balance = balance;
notify_observer(); // 只要余额变动,就向所有注册了的Observer发出通知
}
double get_balance()
{
return balance;
}
};
class ATM : public Observer
{
public:
ATM(Account *account):Observer(account) // 将account传递给基类Observer的构造函数
{
}
~ATM()
{
cout << "in the destructor of Observer..." << endl;
}
public:
void handle_event()
{
cout << "ATM: The balance of bank account is changed to: " << account->get_balance() << endl;
}
void set_account_balance(double balance) // 在ATM中改动银行账户余额,比如在ATM机上存取款
{
account->set_balance(balance);
}
};
class InternetBankingService : public Observer
{
public:
InternetBankingService(Account *account):Observer(account) // 将account传递给基类Observer的构造函数
{
}
~InternetBankingService()
{
cout << "in the destructor InternetBankingService..." << 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);
}
};
class MobileBankingService : public Observer
{
public:
MobileBankingService(Account *account):Observer(account) // 将account传递给基类Observer的构造函数
{
}
~MobileBankingService()
{
cout << "in the destructor of MobileBankingService..." << endl;
}
public:
void handle_event()
{
cout << "MBS: The balance of bank account is changed to: " << account->get_balance() << endl;
}
void set_account_balance(double balance) // 手机银行改动银行账户余额,比如通过网上银行转账
{
account->set_balance(balance);
}
};
// Observer.cpp
#include "Observer.h"
int main(int argc, char **argv)
{
Account *account = new BankAccount();
Observer *atm_observer = new ATM(account);
Observer *ibs_observer = new InternetBankingService(account);
Observer *mbs_observer = new MobileBankingService(account);
// 注册Observer
account->add_observer(atm_observer);
account->add_observer(ibs_observer);
account->add_observer(mbs_observer);
// 银行账户余额变动(比如在柜台存取款)
account->set_balance(1000.12);
// ATM机触发银行账户余额变动
cout << "\nEvent triggered by atm_observer: " << endl;
cout << "================================" << endl;
atm_observer->set_account_balance(1000.23);
// 网上银行触发银行账户余额变动
cout << "\nEvent triggered by ibs_observer: " << endl;
cout << "================================" << endl;
ibs_observer->set_account_balance(1000.36);
// 手机银行触发银行账户余额变动
cout << "\nEvent triggered by mbs_observer: " << endl;
cout << "================================" << endl;
mbs_observer->set_account_balance(1000.68);
// 删除(unsubscribe)网上银行Observer,并变动银行账户余额
cout << "\nRemove ibs_observer: " << endl;
cout << "====================" << endl;
account->remove_observer(ibs_observer);
account->set_balance(2000.23);
cout << "\nDestroy all dynamically created objects: " << endl;
cout << "========================================" << endl;
delete account;
delete atm_observer;
delete ibs_observer;
delete mbs_observer;
}
运行结果:
ATM: The balance of bank account is changed to: 1000.12
IBS: The balance of bank account is changed to: 1000.12
MBS: The balance of bank account is changed to: 1000.12
Event triggered by atm_observer:
================================
ATM: The balance of bank account is changed to: 1000.23
IBS: The balance of bank account is changed to: 1000.23
MBS: The balance of bank account is changed to: 1000.23
Event triggered by ibs_observer:
================================
ATM: The balance of bank account is changed to: 1000.36
IBS: The balance of bank account is changed to: 1000.36
MBS: The balance of bank account is changed to: 1000.36
Event triggered by mbs_observer:
================================
ATM: The balance of bank account is changed to: 1000.68
IBS: The balance of bank account is changed to: 1000.68
MBS: The balance of bank account is changed to: 1000.68
Remove ibs_observer:
====================
ATM: The balance of bank account is changed to: 2000.23
MBS: The balance of bank account is changed to: 2000.23
Destroy all dynamically created objects:
========================================
in the destructor of BankAccount...
in the destructor of Account...
in the destructor of Observer...
in the destructor of Observer...
in the destructor InternetBankingService...
in the destructor of Observer...
in the destructor of MobileBankingService...
in the destructor of Observer...
Observer设计模式和Mediator设计模式的主要区别
Observer设计模式:某个特定的对象(subject或者observable)的状态发生改变时,其他对象(observers)将做出相应的反应。
Mediator设计模式:任何对象的状态发生变化,其他对象都将做出相应的反应,中间是通过Mediator来进行协调的。
Observer设计模式有一个实际意义上的“中心”。Mediator设计模式没有实际意义上的中心,相对而言更分布式一些,其逻辑上的中心是由于纯设计上的需要而引入的“Mediator”,即Observer模式中,事件源在subject或observable对象;在Mediator模式中,事件源在除Mediator之外的其他所有对象。
关于在STL容器中保存对象指针的情况,详见:
http://patmusing.blog.163.com/blog/static/13583496020101831514657/