观察者模式

第一版,耦合比较严重



//.h
#include 
#include 
#include 
#include 

using namespace std;

class StockObserver;



class Secretary {
private:
    
    list  observers ;
    
    string action;
    
    
public:
    
    void Attach(StockObserver * observer){
        observers.push_back(observer);
    }
    
    void Notify() ;
    
    string getSecretaryAction(){
        return action;
    }
    
    void SecretaryAction(string value) {
        action = value;
    }
    
};


class StockObserver {
    
private:
    string name;
    Secretary * sub;
public:
    StockObserver(string name,Secretary * sub):name(name),sub(sub){};
    
    void update(){
        cout<<"close the website ,back work"<::iterator itor;
        itor = observers.begin();
        while (itor != observers.end()) {
            (*(*itor)).update();
            itor++;
        }
}

第二版,观察者抽象


#include 
#include 
#include 
#include 

using namespace std;

class Secretary;

class Observer {
protected:
    string name;
    Secretary * sub;
    
public:
    Observer(string name,Secretary * sub):name(name),sub(sub){};
    ~Observer(){};
    virtual void Update(){};
};


class Secretary  {
private:
    
    list  observers ;
    
    string action;
    
    
public:
    
    void Attach(Observer * observer){
        observers.push_back(observer);
    }
    
    void Detach(Observer * observer) {
        observers.remove(observer);
    }
    
    void Notify() ;
    
    string getSecretaryAction(){
        return action;
    }
    
    void SecretaryAction(string value) {
        action = value;
    }
    
};


class StockObserver:public Observer {

public:
    StockObserver (string name,Secretary * sub):Observer(name,sub){};
    void Update(){
        cout<getSecretaryAction()<getSecretaryAction()<::iterator itor;
        itor = observers.begin();
        while (itor != observers.end()) {
            (*(*itor)).Update();
            itor++;
        }
}



第三次解耦 观察者和通知者都分离了


#include 
#include 
#include 
#include 

using namespace std;

class Subject;

class Observer {
protected:
    string name;
    Subject * sub;
    
public:
    Observer(string name,Subject * sub):name(name),sub(sub){};
    ~Observer(){};
    virtual void Update(){};
};

class Subject {
    
public:
    virtual void Attach(Observer * observer){};
    virtual void Detach(Observer * observer){};
    virtual void Notify(){};
    virtual string getSubjectState(){ return nullptr;};
    virtual void SubjectState(string substate){};
};

class Boss:public Subject {

private:
    list observers;
    string action;
    

public:
    void Attach(Observer * observer){
        observers.push_back(observer);
    }
    
    void Detach(Observer * observer) {
        observers.remove(observer);
    }
    
    void Notify() {
        list::iterator itor;
        itor = observers.begin();
        while (itor != observers.end()) {
            (*(*itor)).Update();
            itor++;
        }
    }
    string getSubjectState(){ return action;}
    
    void SubjectState(string substate){action = substate;};
  
};

class StockObserver:public Observer {

public:
    StockObserver (string name,Subject * sub):Observer(name,sub){};
    void Update(){
        cout<getSubjectState()<getSubjectState()<

重要概念

将一个系统分割成一系列相互协作的类有一个很不好的副作用,就是需要维护相关对象间的一致性,我们不希望为了维持一致性而使各类紧密耦合。

当一个对象不知道具体有多少对象会受到改变时,考虑使用观察者模式
观察者模式主要的工作就是解除耦合,让耦合的双方都依赖于抽象,而不是依赖于具体,从而使各自的变化都不会影响另一边的变化

你可能感兴趣的:(观察者模式)