《HeadFirst设计模式》学习笔记——观察者模式

观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are updated automatically.


Design Principles(设计原则):
1.Encapsulate what varies.
封装变化。

2.Program to interface, not inplementation.
针对接口编程,而不是针对实现编程。

3.Favor composition over inheritance.
多用组合,少用继承。

4.Strive for loosely coupled designs between objects that interact.
为了交互对象之间的松耦合设计而努力。
Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the interdependency between objects.
松耦合的设计之所以能让我们建立有弹性的OO系统,能够应对变化,是因为对象之间的互相依赖性降到了最低。

5.Classes should not open for extension, but closed for modification.
类应该对扩展开放,对修改关闭。
Following the Open-Closed Principle usually introduces new levels of abstraction, whitch adds complexity to our code. You want to concentrate on those areas that are most likely to change in your designs and apply the principles there.
遵循开放-关闭原则,通常会引入新的抽象层次,增加代码的复杂度。你需要把注意力集中在设计中最有可能改变的地方,然后应用开发-关闭原则。


要点:
1.The Observer Pattern defines a one-to-many relationship between objects.
观察者模式定义了对象之间一对多的关系。

2.Subjects, or as we also know them, Observerables, update Observers using a common interface.
主题(可被观察者)用一个共同的接口更新观察者。

3.Observers are loosely coupled in that the Observerable knows nothing about them, other than that they implement the Observer Interface.
观察者和可观察者之间用松耦合方式结合,可观察者不知道观察者的细节,只知道观察者实现了观察者接口。

4.You can push or pull data from the Observerable when using the pattern (pull is considered more "corrct").
使用观察者模式时,可以从被观察者处“推”或“拉”数据(然而,推的方式被认为更“正确”)。

5.Don't depend on a specific order or notification for your Observers.
有多个观察者时,不可以依赖特定的通知次序。


实例:
建立气象站系统。类WeatherData产生三个测量值:温度、湿度与气压,当有新的测量值,立即通知布告板,布告板用户显示天气状况信息。系统须可扩展,可定制布告板。建立三个布告板:“目前状况”, “气象统计”,“天气预报”。

实现:
主题接口(可被观察者):
class CObserver;
class CSubject  
{
public:
CSubject();
virtual ~CSubject();
public:
virtual void RegisterObserver(CObserver *pObserver) = 0;
virtual void UnregisterObserver(CObserver *pObserver) = 0;
virtual void NotifyObservers() = 0;
};

观察者接口:
class CObserver  
{
public:
CObserver();
virtual ~CObserver();
public:
virtual void Update(float temp, float humidity, float pressure) = 0;
};

主题类(可被观察者):
class CWeatherData : public CSubject  
{
public:
void SetMeasurements(float temperature, float humidity, float pressure);
void MeasurementsChanged();
virtual void NotifyObservers();
virtual void UnregisterObserver(CObserver *pObserver);
virtual void RegisterObserver(CObserver *pObserver);
CWeatherData();
virtual ~CWeatherData();
private:
float m_fTemperature;
float m_fHumidity;
float m_fPressure;
//CList <CObserver, CObserver&> m_ObserverList;
CPointerList m_ObserverList;
};

观察者类:
class CCurrentConditionsDisplay : 
public CObserver, 
public CDisplayElement  
{
public:
CCurrentConditionsDisplay(CSubject *weatherData);
virtual ~CCurrentConditionsDisplay();
virtual void Update(float temp, float humidity, float pressure);
virtual void Display();
private:
float m_fTemperature;
float m_fHumidity;
float m_fPressure;
CSubject *m_weatherData;
};

测试:
int main(int argc, char* argv[])
{
CWeatherData wd;
CCurrentConditionsDisplay ccd(&wd);
CStatisticsDisplay sd(&wd);
CForecastDisplay fd(&wd);
CHeatIndexDisplay hid(&wd);

wd.SetMeasurements(80, 65, 30.4f);
printf("\n\n");
wd.SetMeasurements(82, 70, 29.2f);
printf("\n\n");
wd.SetMeasurements(78, 90, 29.2f);
printf("\n\n");

printf("Hello World!\n");
system("pause");
return 0;
}

结果:
Current conditions: 80.00F degree, 65.00% humidity and 30.40 pressure
Avg/Max/Min temperature = 80.00/80.00/80.00
Forecast: Improving weather on the way!
Heat index is 82.95535


Current conditions: 82.00F degree, 70.00% humidity and 29.20 pressure
Avg/Max/Min temperature = 81.00/82.00/80.00
Forecast: Watch out for cooler, rainy weather
Heat index is 86.90123


Current conditions: 78.00F degree, 90.00% humidity and 29.20 pressure
Avg/Max/Min temperature = 80.00/82.00/78.00
Forecast: More of the same
Heat index is 83.64967


请按任意键继续. . .


完整源代码下载: http://download.csdn.net/detail/captainwong/5604329
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值