码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式——5.观察者模式

时间:2018-10-05 20:21:27      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:iterator   turn   观察者   设计   subject   ras   vat   contains   ram   

观察者模式(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.观察者模式

标签:iterator   turn   观察者   设计   subject   ras   vat   contains   ram   

原文地址:https://www.cnblogs.com/sylvan/p/9745669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!