One of c ++ design patterns: Observer Pattern

Observer Pattern

In the GOF "Design Patterns: Elements of Reusable Object-Oriented Software" book mode observer is to say: the definition of the dependencies between the subject an-many, when a state of the object changes, All objects that depend on it have been notified and updated automatically. When an object has changed, its focus will be notified of the object; this interaction is also known as publish - subscribe (publish-subscribe). The goal is to inform the publisher, when it issued a notice does not need to know who its viewers.

Let me say the relationship between the above data and; whether it is a line chart, pie charts or bar charts, they all rely on the data; when the data changes, the data object is dependent on its notification to update the object; so there Excel, when data changes, the corresponding chart will automatically redraw.

 

UML 类图

Subject
(target) - Target knows its viewers. There may be any number of observers to observe the same goal;
- providing an interface to register and delete observer objects.

Observer (observers)
- defined as those objects to be notified of a change in the target update interface.

ConcreteSubject (Target)
- a relevant status of an object stored in each ConcreteObserver;
- when it changes state, from the notification of the respective observer.

ConcreteObserver (specific viewer)
- ConcreteSubject maintains a pointer to the object references;
- storing information about states, which should be consistent with the state of the target;
- Observer update interface to achieve a state of its state consistent with the objectives.

The observer pattern to collaborate in the following ways:

  1. When any possible ConcreteSubject viewer with itself lead to an inconsistent state changes, it notifies its respective viewer;
  2. After getting notice a change targets, ConcreteObserver object can query information to the target object. ConcreteObserver use this information to state that its state is consistent with the target object.

The following is a timing diagram call:

 

Using the occasion

Observer mode can be used in any of the following:

  1. When an abstract model has two aspects, one aspect of which is dependent on the other hand. Both these encapsulated in a separate object so that they can independently change and reuse;
  2. When changing to an object other objects need to change, but do not know exactly how many objects need to be changed;
  3. When an object must notify other objects, but it can not assume that anyone other objects are; that is, you do not want these objects are tightly coupled.

 

Code:

#include<iostream>
#include<list>
using namespace std;

//观察者
class Observer
{
public:
	virtual void Update(int) = 0;
};

//被观察的目标
class Subject
{
public:
	virtual void Attach(Observer *) = 0;
	virtual void Detach(Observer *) = 0;
	virtual void Notify() = 0;
};


//具体的观察者
class ConcreteObserver :public Observer
{
public:
	ConcreteObserver(Subject* pSubject)
	{
		m_pSubject = pSubject;
	}

	void Update(int value)
	{
		cout << "ConcreteObServer get the update, new State" << value << endl;
	}

private:
	Subject* m_pSubject;  //所要观察的目标
};


//具体的观察者2
class ConcreteObserver2 :public Observer
{
public:
	ConcreteObserver2(Subject* pSubject)
	{
		m_pSubject = pSubject;
	}

	void Update(int value)
	{
		cout << "ConcreteObserver2 get the update, new State"<<value<<endl;
	}
private:
	Subject* m_pSubject;
};



class ConcreteSubject :public Subject
{
public:
	void Attach(Observer* pObserver);
	void Detach(Observer* pObserver);
	void Notify();

	void SetState(int state)
	{
		m_iState = state;
	}

private:
	list<Observer* > m_ObserverList;   //存储观察者
	int m_iState;
};

void ConcreteSubject::Attach(Observer* pObserver)  //注册观察者
{
	m_ObserverList.push_back(pObserver);
}

void ConcreteSubject::Detach(Observer* pObserver)   //取消观察者
{
	m_ObserverList.remove(pObserver);
}

void ConcreteSubject::Notify()
{

	list<Observer*>::iterator it = m_ObserverList.begin();
	while (it != m_ObserverList.end())
	{
		(*it)->Update(m_iState);
		++it;
	}
}

int main()
{
	// Create Subject
	ConcreteSubject* pSubject = new ConcreteSubject();

	// Create Observer
	Observer* pObserver = new ConcreteObserver(pSubject);
	Observer* pObserver2 = new ConcreteObserver2(pSubject);

	// Change the state
	pSubject->SetState(2);

	// Register the observer
	pSubject->Attach(pObserver);
	pSubject->Attach(pObserver2);

	pSubject->Notify();

	// Unregister the observer
	pSubject->Detach(pObserver);

	pSubject->SetState(3);
	pSubject->Notify();

	delete pObserver;
	delete pObserver2;
	delete pSubject;



}

to sum up

Mode observer status in the 23 design patterns is very high, we are basically the major framework everywhere. The real understanding of the whole pattern, other people's code has a very big help for us to understand; will use more or less of the design patterns in our future work. I came up with is not very comprehensive, in the future, if you encounter a need to add content, I will continue to add; also hope that we come up with better suggestions.

He published 199 original articles · won praise 77 · Views 200,000 +

Guess you like

Origin blog.csdn.net/tianguiyuyu/article/details/103945237