设计模式(10)--观察者模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式(10)--观察者模式相关的知识,希望对你有一定的参考价值。

//10.观察者模式
//ver1
class StockObserver; //与 Secretary 相互依赖; 所以提前声明
class Secretary;

//股票同事类
class StockObserver
{
private:
	string _name;
	Secretary * _ps;
public:
	StockObserver(string name, Secretary *p)
	{
		_name = name;
		_ps = p;
	}
	void Update()
	{
		//得到前台通知,赶快采取行动。
		//_ps->GetSecretaryAction(); 
		//这里必须屏蔽; 这里没法编译通过, StockObserver 与 Secretary 相互耦合。
		//解决的办法是,分别写在2个.h和.cpp文件中。并在.h文件里提前声明要引用的类,并把对象定义为指针。
	}
};

//前台秘书类
class Secretary
{
private:
	list<StockObserver *> myList;
	string _action;
public:
	//增加
	void Attach(StockObserver * psob)
	{
		myList.push_back(psob);
	}
	//减少
	void Detach(StockObserver * psob)
	{
		myList.remove(psob);
	}
	//通知
	void Notify()
	{
		list<StockObserver *>::iterator it = myList.begin();
		for ( ; it != myList.end(); ++it )
		{
			(*it)->Update();
		}
	}
	//前台状态
	void SetSecretaryAction(string strAct)
	{
		_action = strAct;
	}
	string GetSecretaryAction()
	{
		return _action;
	}
};

void main1()
{
	Secretary *ps1 = new Secretary();
	StockObserver *pso1 = new StockObserver("fun", ps1);
	StockObserver *pso2 = new StockObserver("test", ps1);

	//前台记下两位同事
	ps1->Attach((pso1));
	ps1->Attach((pso2));

	//老板回来了,发通知
	ps1->SetSecretaryAction("老板回来了");
	ps1->Notify();
}

 

//10.观察者模式
//ver2

//Observer.h

//前置声明
class Subject; 
class Boss;
class Secretary;

class Observer
{
protected:
	string _name;
	Subject * _ps;

public:
	Observer(string name, Subject * ps);
	virtual ~Observer(void);
public:
	virtual void Update();
};

//看股票同事
class StockObserver : public Observer
{
public:
	StockObserver(string name, Subject * ps);
	virtual ~StockObserver();

	virtual void Update();
};
//看NBA同事
class NBAObserver : public Observer
{
public:
	NBAObserver(string name, Subject * ps);
	virtual ~NBAObserver();

	virtual void Update();
};

//Observer.cpp
#include "Observer.h"
#include "Secretary.h"

Observer::Observer(string name, Subject * ps)
{
	_ps = ps;
	_name = name;
}

Observer::~Observer(void)
{
}

void Observer::Update()
{
	_ps->GetSecretaryAction();
}

StockObserver::StockObserver(string name, Subject * ps)
	: Observer(name, ps)
{}

StockObserver::~StockObserver()
{}

void StockObserver::Update()
{
	_ps->GetSecretaryAction();
}

///////////////////////////////
NBAObserver::NBAObserver(string name, Subject * ps) 
	: Observer(name, ps)
{}

NBAObserver::~NBAObserver()
{}

void NBAObserver::Update()
{
	_ps->GetSecretaryAction();
}

//-----------------------------------
//Subject.h

//前置声明
class Observer;
class StockObserver;
class NBAObserver;

//通知者接口
class Subject
{
public:
	virtual void Attach(Observer *pob);
	virtual void Detach(Observer *pob);
	virtual void Notify();
	void SetSecretaryAction(string strAct);
	string GetSecretaryAction();
};

//老板通知者
class Boss : public Subject
{
private:
	list<Observer*> myList;
	string _action;

public:
	void Attach(Observer *pob);
	void Detach(Observer *pob);
	void Notify();
	void SetSecretaryAction(string strAct);
	string GetSecretaryAction();
};

class Secretary
{
private:
	list<Observer*> myList;
	string _action;

public:
	//增加
	void Attach(Observer * pob);
	//减少
	void Detach(Observer * pob);
	//通知
	void Notify();
	//前台状态
	void SetSecretaryAction(string strAct);
	string GetSecretaryAction();
};

//Subject.cpp
#include "Secretary.h"
#include "Observer.h"

void Subject::Attach(Observer *pob){}
void Subject::Detach(Observer *pob){}
void Subject::Notify(){}
void Subject::SetSecretaryAction(string strAct){}
string Subject::GetSecretaryAction(){return "";}

////////////////

void Boss::Attach(Observer *pob)
{
	myList.push_back(pob);
}
void Boss::Detach(Observer *pob)
{
	myList.remove(pob);
}
void Boss::Notify()
{
	list<Observer *>::iterator it = myList.begin();
	for ( ; it != myList.end(); ++it )
	{
		(*it)->Update();
	}
}
void Boss::SetSecretaryAction(string strAct)
{
	_action = strAct;
}
string Boss::GetSecretaryAction()
{
	return _action;
}
///////////////
void Secretary::Attach(Observer * pob)
{
	myList.push_back(pob);
}
//减少
void Secretary::Detach(Observer * pob)
{
	myList.remove(pob);
}
//通知
void Secretary::Notify()
{
	list<Observer *>::iterator it = myList.begin();
	for ( ; it != myList.end(); ++it )
	{
		(*it)->Update();
	}
}
//前台状态
void Secretary::SetSecretaryAction(string strAct)
{
	_action = strAct;
}
string Secretary::GetSecretaryAction()
{
	return _action;
}

//-----------------
//main.cpp
#include "Secretary.h"
#include "Observer.h"

int main()
{
	//老板
	Boss *pBoss = new Boss();

	//看股票同事
	StockObserver *pTongshi1 = new StockObserver("fun", pBoss);
	//看NBA同事
	NBAObserver *pTongshi2 = new NBAObserver("Test", pBoss);

	pBoss->Attach(pTongshi1);
	pBoss->Attach(pTongshi2);

	pBoss->Detach(pTongshi1);

	//老板回来
	pBoss->SetSecretaryAction("我是老板我回来了!");
	//发出通知
	pBoss->Notify();
	return 0;
}

 

 

 

 

以上是关于设计模式(10)--观察者模式的主要内容,如果未能解决你的问题,请参考以下文章

Java设计模式补充:回调模式事件监听器模式观察者模式(转)

观察者模式和发布/订阅模式的区别

设计模式(10)--观察者模式

java观察者模式代码,含答案解析

大厂面试必备之设计模式:漫画观察者模式

如何以纯功能的方式实现观察者设计模式?