`
收藏列表
标题 标签 来源
装饰者模式 装饰者模式
/*装饰者模式测试代码*/
#include <iostream>

using std::cout;
using std::endl;

class Building
{
    public:
        virtual void appearance() = 0;
};

class HouseInBeiJing : public Building
{
    public:
        void appearance()
        {
            cout << "&-BeiJing<- The house ->BeiJing-&" << endl; 
        }
};

class Fishing : public Building
{
    public:
        virtual void appearance() = 0;
    protected:
        Building *_pBuilding;
};

class Color : public Fishing
{
    public:
       Color(Building *ptrFishing){ _pBuilding = ptrFishing;}
    public:
       void appearance()
       {
            cout << "===========Color===========" << endl;
            _pBuilding->appearance();
            cout << "===========Color===========" << endl;
       }
};

class Window : public Fishing
{
    public:
        Window(Building *ptrBuilding) { _pBuilding = ptrBuilding; }
        void appearance()
        {
            cout << "+++++++++++Window+++++++++++" << endl;
            _pBuilding->appearance();
            cout << "+++++++++++Window+++++++++++" << endl;
        }
};

int main(int argc, char* argv[])
{
    Building* hibj = new Window(new Color(new HouseInBeiJing));  
    hibj->appearance();

    return 0;
C++实现简单的观察者模式 观察者模式
// 
// easy_test.cpp
//  develop by ZhouFeng , Mar 6th 2013
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

class IObserver;

class IObserable 
{
    public:
        virtual void addObserver(IObserver*) = 0; 
        virtual void removeObserver(IObserver*) = 0;
        virtual void notifyObserver() = 0;
};

class IObserver
{
    public:
        virtual void update() = 0;
};

class Boss : public IObserable
{
    typedef vector<IObserver*>::iterator it_obs;
    typedef vector<IObserver*> t_vobs;
    public:
        Boss()
            : vobs(new t_vobs)
        {}

        ~Boss()
        {
            delete vobs; 
        }

        void addObserver(IObserver *obs)
        {
            vobs->push_back(obs); 
        }

        void removeObserver(IObserver *obs)
        {
            for(int i = 0; i < vobs->size(); ++i) 
            {
                if((*vobs)[i] == obs) 
                {
                    vobs->erase(vobs->begin() + i); 
                    break;
                }
            }
        }

        void notifyObserver()
        {
            for(int i = 0; i < vobs->size(); ++i)
            {
                (*vobs)[i]->update();
            }
        }
    private:
       t_vobs* vobs;
};

class TechnologicalLeader : public IObserver
{
    public:
        TechnologicalLeader(string name)
        {
            _name = name; 
        }
       
        void update()
        {
            cout << "TechnologicalLeader:" << _name << endl;
            cout << "Yes, I have got it.Thank you!" << endl; 
            cout << "--End--" << endl;
        }
    private:
        string _name;
};

int main(int argc, char* argv[])
{
    IObserable *obs = new Boss;
    IObserver *observer = new TechnologicalLeader("JunLing.Wu");
    IObserver *observer2 = new TechnologicalLeader("YouZhen.Wang");
    obs->addObserver(observer);
    obs->addObserver(observer2);
    obs->notifyObserver();
    obs->removeObserver(observer);
    obs->notifyObserver();

    return 0;
}
Global site tag (gtag.js) - Google Analytics