前言
这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可。
开发环境:WIN7 32位 + VS2010
发现在VS2005中使用std::funtion报错:
错误 1 error C2039: “function”: 不是“std”的成员 e:\vsprojectsforvms\designpattern\observer2\observer2.cpp 123
于是改为VS2010来写。
#include "stdafx.h"
//std::function需要此头文件
#include
#include
#include
//std::find需要此头文件
#include
using namespace std;
/********************************************
First类和Second相当于两个观察者, 他们两个没有继承结构
First类和Second类的更新函数原型相同,函数名不必相同
********************************************/
class First
{
public:
int Add1(int a, int b) //更新函数
{
return a + b;
}
};
class Second
{
public:
int Add2(int a, int b) /