• Observer


    observer.h
    #ifndef OBSERVE_H
    #define OBSERVE_H
    #include "iostream"
    using namespace std;
    
    class Observer
    {
    public:
        Observer();
        virtual void update(int hour, int min, int sec) = 0;
    };
    
    #endif // OBSERVE_H
    observer.cpp
    #include "observer.h"
    
    Observer::Observer()
    {
    
    }
    subject.h
    #ifndef SUBJECT_H
    #define SUBJECT_H
    #include <list>
    #include "observer.h"
    using namespace std;
    
    class Subject
    {
    public:
        Subject();
        void registerObserver(Observer *ob);
        void removeObserver(Observer *ob);
    
        virtual void notify() = 0;
    protected:
        list<Observer *> lo;
    };
    
    #endif // SUBJECT_H
    subject.cpp
    #include "subject.h"
    
    Subject::Subject()
    {
    
    }
    
    void Subject::registerObserver(Observer *ob)
    {
        lo.push_back(ob);
    }
    
    void Subject::removeObserver(Observer *ob)
    {
        lo.remove(ob);
    }
    bejingtime.h
    #ifndef BEIJINGTIME_H
    #define BEIJINGTIME_H
    #include "subject.h"
    
    class BeijingTime : public Subject
    {
    public:
        BeijingTime();
        void setTime(int hour, int min, int sec);
        void notify();
        void dis()
        {
            cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl;
        }
    
    protected:
        int _hour;
        int _min;
        int _sec;
    };
    
    #endif // BEIJINGTIME_H
    beijingtime.cpp
    #include "beijingtime.h"
    
    BeijingTime::BeijingTime()
    {
    
    }
    
    void BeijingTime::notify()
    {
        list<Observer *>::iterator itr;
        for(itr = lo.begin(); itr != lo.end(); ++itr)
        {
            (*itr)->update(_hour, _min, _sec);
        }
    }
    
    void BeijingTime::setTime(int hour, int min, int sec)
    {
        _hour = hour;
        _min = min;
        _sec = sec;
        notify();
    }
    canadatime.h
    #ifndef CANADATIME_H
    #define CANADATIME_H
    #include "observer.h"
    
    class CanadaTime : public Observer
    {
    public:
        CanadaTime();
        void update(int hour, int min, int sec);
        void dis()
        {
            cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl;
        }
    private:
        int _hour;
        int _min;
        int _sec;
    };
    
    #endif // CANADATIME_H
    canadatime.cpp
    #include "canadatime.h"
    
    CanadaTime::CanadaTime()
    {
    
    }
    
    void CanadaTime::update(int hour, int min, int sec)
    {
        _hour = hour + 100;
        _min = min;
        _sec = sec;
    }
    londontime.h
    #ifndef LONDONTIME_H
    #define LONDONTIME_H
    #include "observer.h"
    
    class LondonTime : public Observer
    {
    public:
        LondonTime();
        void update(int hour, int min, int sec);
        void dis()
        {
            cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl;
        }
    private:
        int _hour;
        int _min;
        int _sec;
    };
    
    #endif // LONDONTIME_H
    londontime.cpp
    #include "londontime.h"
    
    LondonTime::LondonTime()
    {
    
    }
    
    void LondonTime::update(int hour, int min, int sec)
    {
        _hour = hour + 200;
        _min = min;
        _sec = sec;
    }
    tokyotime.h
    #ifndef TOKYOTIME_H
    #define TOKYOTIME_H
    #include "observer.h"
    
    class TokyoTime : public Observer
    {
    public:
        TokyoTime();
        void update(int hour, int min, int sec);
        void dis()
        {
            cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl;
        }
    private:
        int _hour;
        int _min;
        int _sec;
    };
    
    #endif // TOKYOTIME_H
    tokyotime.cpp
    #include "tokyotime.h"
    
    TokyoTime::TokyoTime()
    {
    
    }
    
    void TokyoTime::update(int hour, int min, int sec)
    {
        _hour = hour + 300;
        _min = min;
        _sec = sec;
    }
    main.cpp
    #include <iostream>
    #include "tokyotime.h"
    #include "londontime.h"
    #include "canadatime.h"
    #include "beijingtime.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        TokyoTime tt;
        LondonTime lt;
        CanadaTime ct;
        BeijingTime bt;
        bt.registerObserver(&tt);
        bt.registerObserver(&lt);
        bt.registerObserver(&ct);
        bt.setTime(100, 200, 300);
        bt.dis();
        tt.dis();
        lt.dis();
        ct.dis();
        return 0;
    }
  • 相关阅读:
    Divide Two Integers
    LRU Cache
    Max Points on a Line
    Recover Binary Search Tree
    Copy List with Random Pointer
    IEE修改最大连接数
    SQL Tuning 基础概述02
    RHEL 本地yum源配置
    Linux演示 dd测试IO
    Linux平台oracle 11g单实例 安装部署配置 快速参考
  • 原文地址:https://www.cnblogs.com/aelite/p/9847072.html
Copyright © 2020-2023  润新知