设计模式-Subject(行为模式)

以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码

//Subject.h

#pragma once

#include<list>
#include<string>

typedef std::string State;

class Observer;
class Subject
{
public:
    virtual ~Subject();
    virtual void Attach(Observer* obv);
    virtual void Detach(Observer* obv);
    virtual void Notify();
    virtual void SetState(const State& st) = 0;
    virtual State GetState() = 0;
protected:
    Subject();
private:
    std::list<Observer*>*_obs;
};

class ConcreateSubject :public Subject
{
public:
    ConcreateSubject();
    ~ConcreateSubject();
    void SetState(const State& st);
    State GetState();
protected:
private:
    State _st;
};

//Subject.cpp

#include"Subject.h"
#include"Observer.h"

#include<iostream>
#include<list>

typedef std::string state;
Subject::Subject()
{
    _obs = new std::list<Observer*>;
}

Subject::~Subject()
{}
void Subject::Attach(Observer* obs)
{
    _obs->push_front(obs);
}
void Subject::Detach(Observer* obs)
{
    if (obs != NULL)
        _obs->remove(obs);
}
void Subject::Notify()
{
    for (std::list<Observer*>::iterator it = _obs->begin(); it != _obs->end(); ++it)
    {
        (*it)->Update(this);
    }
}

ConcreateSubject::ConcreateSubject()
{
    _st = "\0";
}
ConcreateSubject::~ConcreateSubject()
{

}
state ConcreateSubject::GetState()
{
    return _st;
}

void ConcreateSubject::SetState(const state& st)
{
    _st = st;
}

//Observer.h

#include"Subject.h"
#include<string>
typedef std::string state;
class Observer
{
public:
    virtual ~Observer();
    virtual void Update(Subject* sub) = 0;
    virtual void PrintInfo() = 0;
protected:
    Observer();
    State _st;
private:
};

class ConcreateObserverA :public Observer
{
public:
    virtual Subject* GetSubject();
    ConcreateObserverA(Subject* sub);
    virtual ~ConcreateObserverA();
    void Update(Subject* sub);
    void PrintInfo();
protected:
private:
    Subject* _sub;

};

class ConcreateObserverB: public Observer
{
public:
    virtual Subject* GetSubject();
    ConcreateObserverB(Subject* sub);
    virtual ~ConcreateObserverB();
    void Update(Subject* sub);
    void PrintInfo();
protected:
private:
    Subject* _sub;
};

//Observer.cpp

#include"Observer.h"
#include"Subject.h"

#include<iostream>
#include<string>

Observer::Observer(){
    _st = '\0';
}
Observer::~Observer(){}

ConcreateObserverA::ConcreateObserverA(Subject* sub)
{
    _sub = sub;
    _sub->Attach(this);
}
ConcreateObserverA::~ConcreateObserverA()
{
    _sub->Detach(this);
    if (_sub != 0)
        delete _sub;
}

Subject* ConcreateObserverA::GetSubject()
{
    return _sub;
}

void ConcreateObserverA::PrintInfo()
{
    std::cout << "ConcreateObserverA..." << _sub->GetState() << std::endl;

}

void ConcreateObserverA::Update(Subject* sub)
{
    _st = sub->GetState();
    PrintInfo();
}

ConcreateObserverB::ConcreateObserverB(Subject* sub)
{
    _sub = sub;
    _sub->Attach(this);
}

ConcreateObserverB::~ConcreateObserverB()
{
    _sub->Detach(this);
    if (_sub != 0)
        delete _sub;
}

Subject* ConcreateObserverB::GetSubject()
{
    return _sub;
}
void ConcreateObserverB::PrintInfo()
{
    std::cout << "ConcreateObserverB..." << _sub->GetState() << std::endl;
}
void ConcreateObserverB::Update(Subject* sub)
{
    _st = sub->GetState();
    PrintInfo();
}

//main.cpp

#include"Observer.h"
#include"Subject.h"
#include<iostream>
#include<string>

int main(int args, char* argv)
{
    ConcreateSubject* sub = new ConcreateSubject();
    Observer* o1 = new ConcreateObserverA(sub);
    Observer* o2 = new ConcreateObserverB(sub);
    sub->SetState("old");
    sub->Notify();
    sub->SetState("new");
    sub->Notify();
    getchar();
    return 0;
}
posted @ 2019-12-23 10:50  fourmii  阅读(397)  评论(0编辑  收藏  举报