观察者模式(Observer)
观察者模式(Observer)简介:
定义一个一对多的依赖关系,让多个观察者对象监听某个主题对象,当主题对象的状态发生改变时,主题对象则通知所有的观察者对象,做出相应的反应,更新自己的状态等。
观察者模式结构图:
C++代码
Subject抽象类以及ConcreteSubject具体实现类:
//file: Subject.h#pragma once#include "Observer.h"#include <vector>using namespace std;class Subject{public:Subject();virtual ~Subject();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;};class ConcreteSubject : public Subject{public:ConcreteSubject();virtual ~ConcreteSubject();virtual int getState();virtual void setState(int i);private:int m_State;};
//file: Subject.cpp#include "pch.h"#include "Subject.h"//SubjectSubject::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);}}//ConcreteSubjectConcreteSubject::ConcreteSubject() {}ConcreteSubject::~ConcreteSubject() {}int ConcreteSubject::getState(){return m_State;}void ConcreteSubject::setState(int i){m_State = i;}
Observer抽象类以及ConcreteObserver具体观察者类:
//file: Observer.h#pragma once#include <string>using namespace std;class Subject;class Observer{public:Observer();virtual ~Observer();virtual void update(Subject *ps) = 0;};class ConcreteObserver : public Observer{public:ConcreteObserver(string pName);virtual ~ConcreteObserver();virtual void update(Subject *ps);private:string m_ObjName;int m_ObserverState;};
//file: Observer.cpp#include "pch.h"#include "Subject.h"#include "Observer.h"#include <iostream>using namespace std;//ObserverObserver::Observer() {}Observer::~Observer() {}//ConcreteOberverConcreteObserver::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 "Subject.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();delete sub;delete objA;delete objB;return 0;}
C#代码
观察者模式实现
Subject抽象类以及ConcreteSubject具体实现类:
public abstract class Subject{private List<Observer> objList = new List<Observer>();public int State { get; set; }public void Attach(Observer obj){objList.Add(obj);}public void Detach(Observer obj){objList.Remove(obj);}public void Notify(){foreach (Observer o in objList){o.Update(this);}}}public class ConcreteSubject : Subject{}
Observer抽象类以及ConcreteObserver具体观察者类:
public abstract class Observer{public abstract void Update(Subject sub);}public class ConcreteObserver : Observer{int m_State;string m_Name;public ConcreteObserver(string name){this.m_Name = name;}public override void Update(Subject sub){this.m_State = sub.State;Console.WriteLine("Update Observer {0} to state {1} .", m_Name, m_State);}}
客户端代码:
class Program{static void Main(string[] args){Subject sub = new ConcreteSubject();Observer objA = new ConcreteObserver("A");Observer objB = new ConcreteObserver("B");sub.Attach(objA);sub.Attach(objB);sub.State = 101;sub.Notify();sub.Detach(objB);sub.State = 102;sub.Notify();Console.ReadKey(false);}}
事件委托实现
抽象Subject类以及委托方法原型以及ConcreteSubject具体实现类:
public delegate void EventHander(Subject sub);public abstract class Subject{public int State { get; set; }public abstract void Notify();}public class ConcreteSubject : Subject{public event EventHander Update;public override void Notify(){if (Update != null)Update(this);}}
ConcreteObserverA/B具体实现类:
public class ConcreteObserverA{public int State { get; set; }public string Name { get; set; }public ConcreteObserverA(string name){Name = name;}public void FuncA(Subject sub){this.State = sub.State;Console.WriteLine("Update Observer {0} to state {1} .", Name, State);}public void FeatureA(Subject sub){this.State = sub.State;Console.WriteLine("Update Observer {0} to state {1} .", Name, State);}}public class ConcreteObserverB{public int State { get; set; }public string Name { get; set; }public ConcreteObserverB(string name){Name = name;}public void FuncB(Subject sub){this.State = sub.State;Console.WriteLine("Update Observer {0} to state {1} .", Name, State);}public void FeatureB(Subject sub){this.State = sub.State;Console.WriteLine("Update Observer {0} to state {1} .", Name, State);}}
客户端代码:
class Program{static void Main(string[] args){ConcreteSubject sub = new ConcreteSubject();ConcreteObserverA objA = new ConcreteObserverA("ConcreteObserverA");ConcreteObserverB objB = new ConcreteObserverB("ConcreteObserverB");sub.Update += objA.FuncA;sub.Update += objB.FeatureB;sub.State = 101;sub.Notify();sub.Update -= objB.FeatureB;sub.State = 102;sub.Notify();Console.ReadKey(false);}}
运行实例:
REF
书籍:
设计模式与游戏开发、大话设计模式
GitHub:
https://github.com/me115/design_patterns