设计模式——5.观察者模式
Posted sylvan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式——5.观察者模式相关的知识,希望对你有一定的参考价值。
观察者模式(Observer)简介:
定义一个一对多的依赖关系,让多个观察者对象监听某个主题对象,当主题对象的状态发生改变时,主题对象则通知所有的观察者对象,做出相应的反应,更新自己的状态等。
观察者模式(Observer)结构图:
C++代码:
Subject抽象类:
//file: Subject.h
#pragma once
#include "Observer.h"
#include <vector>
using namespace std;
class Subject
{
public:
Subject();
virtual ~Subject();
Observer *m_Observer;
void attach(Observer *pObserver);
void detach(Observer *pObserver);
void notify();
virtual int getState() = 0;
virtual void setState(int i) = 0;
private:
vector<Observer *> m_vtObj;
};
//file: Subject.cpp
#include "pch.h"
#include "Subject.h"
Subject::Subject() {}
Subject::~Subject() {}
void Subject::attach(Observer *pObserver)
{
m_vtObj.push_back(pObserver);
}
void Subject::detach(Observer *pObserver)
{
for (vector<Observer *>::iterator iter = m_vtObj.begin(); iter != m_vtObj.end(); iter++)
{
if (*iter == pObserver)
{
m_vtObj.erase(iter);
return;
}
}
}
void Subject::notify()
{
for (vector<Observer *>::iterator iter = m_vtObj.begin(); iter != m_vtObj.end(); iter++)
{
(*iter)->update(this);
}
}
Observer抽象类:
//file: Observer.h
#pragma once
class Subject;
class Observer
{
public:
Observer();
virtual ~Observer();
virtual void update(Subject *ps) = 0;
};
//file: Observer.cpp
#include "pch.h"
#include "Observer.h"
Observer::Observer() {}
Observer::~Observer() {}
ConcreteSubject具体主题类:
//file: ConcreteSubject.h
#pragma once
#include "Subject.h"
class ConcreteSubject : public Subject
{
public:
ConcreteSubject();
virtual ~ConcreteSubject();
virtual int getState();
virtual void setState(int i);
private:
int m_State;
};
//file: ConcreteSubject.cpp
#include "pch.h"
#include "ConcreteSubject.h"
ConcreteSubject::ConcreteSubject() {}
ConcreteSubject::~ConcreteSubject() {}
int ConcreteSubject::getState()
{
return m_State;
}
void ConcreteSubject::setState(int i)
{
m_State = i;
}
ConcreteObserver具体观察者类:
//file: ConcreteObserver.h
#pragma once
#include "Observer.h"
#include <string>
using namespace std;
class ConcreteObserver : public Observer
{
public:
ConcreteObserver(string pName);
virtual ~ConcreteObserver();
virtual void update(Subject *ps);
private:
string m_ObjName;
int m_ObserverState;
};
//file: ConcreteObserver.cpp
#include "pch.h"
#include "ConcreteObserver.h"
#include "Subject.h"
#include <iostream>
ConcreteObserver::ConcreteObserver(string pName)
{
m_ObjName = pName;
}
ConcreteObserver::~ConcreteObserver() {}
void ConcreteObserver::update(Subject *ps)
{
m_ObserverState = ps->getState();
cout << "update observer " << m_ObjName << " to state "<<m_ObserverState<< " !" << endl;
}
客户端代码:
//file: ObserverPattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include "pch.h"
#include <iostream>
#include "ConcreteSubject.h"
#include "ConcreteObserver.h"
int main()
{
Subject *sub = new ConcreteSubject();
Observer *objA = new ConcreteObserver("A");
Observer *objB = new ConcreteObserver("B");
sub->attach(objA);
sub->attach(objB);
sub->setState(101);
sub->notify();
sub->detach(objB);
sub->setState(102);
sub->notify();
char c;
cin >> c;
delete sub;
delete objA;
delete objB;
return 0;
}
以上是关于设计模式——5.观察者模式的主要内容,如果未能解决你的问题,请参考以下文章
corda ledger系列教5 观察者模式订阅-发布模式和响应式编程