觀察者模式(Observer)

觀察者模式

定義了對象間的一對多的依賴關係,讓多個觀察者對象同時監聽某一個主題對象(被觀察者)。當主題對象的狀態發生更改時,會通知所有觀察者,讓它們能夠自動更新。

 

動機

  • 在軟件構建的過程中,我們需要爲某些對象建立一種“通知依賴關係”——一個對象(目標對象)的狀態發生改變,所有的以來對象(觀察者對象)都將得到通知。如果這樣的依賴關係過於緊密,將使得軟件不能很好的抵禦變化。
  • 使用面向對象技術,可以將這種依賴關係弱化,並形成一種穩定的依賴關係。從而實現軟件體系結構的鬆耦合。

例:下面是一個文件切割的程序的僞碼,可以把較大的文件分隔爲若干個較小的文件。並且對於文件分割過程提供一個進度條的展示過程,可以實時的現實分割的進度。

class FileSplitter
{
    string m_filePath;
    int m_fileNumber;
    ProgressBar* m_progressBar;
 
public:
    FileSplitter(const string& filePath, int fileNumber, ProgressBar* progressBar) :
        m_filePath(filePath), 
        m_fileNumber(fileNumber),
        m_progressBar(progressBar){
 
    }
 
    void split(){
 
        //1.讀取大文件
 
        //2.分批次向小文件中寫入
        for (int i = 0; i < m_fileNumber; i++){
            //...
            float progressValue = m_fileNumber;
            progressValue = (i + 1) / progressValue;
            m_progressBar->setValue(progressValue);
        }
 
    }
};
 
 
class MainForm : public Form
{
    TextBox* txtFilePath;
    TextBox* txtFileNumber;
    ProgressBar* progressBar;
 
public:
    void Button1_Click(){
 
        string filePath = txtFilePath->getText();
        int number = atoi(txtFileNumber->getText().c_str());
 
        FileSplitter splitter(filePath, number, progressBar);
 
        splitter.split();
 
    }
};

以上的設計方法,前端界面依賴了一個ProgressBar,而前端頁面屬於是易變化的組件。同時,其中的耦合性也很高,只要需要更改顯示的進度的情況,就需要更改前端的界面。

由以上的依賴關係,不難看出,對於分隔文件的算法,相對來說穩定,但是卻依賴了一個不穩定的ProgressBar。那麼如果對於界面修改的部分來說,他面對的是一個通知的數組,每個數組裏面都是一個抽象的通知的接口,就可以使用多態性來解決解耦的問題。

class IProgress{
public:
    virtual void DoProgress(float value)=0;
    virtual ~IProgress(){}
};
 
 
class FileSplitter
{
    string m_filePath;
    int m_fileNumber;
 
    List<IProgress*>  m_iprogressList; // 抽象通知機制,支持多個觀察者
 
public:
    FileSplitter(const string& filePath, int fileNumber) :
        m_filePath(filePath), 
        m_fileNumber(fileNumber){
 
    }
 
 
    void split(){
 
        //1.讀取大文件
 
        //2.分批次向小文件中寫入
        for (int i = 0; i < m_fileNumber; i++){
            //...
 
            float progressValue = m_fileNumber;
            progressValue = (i + 1) / progressValue;
            onProgress(progressValue);//發送通知
        }
 
    }
 
 //subject部分
    void addIProgress(IProgress* iprogress){
        m_iprogressList.push_back(iprogress);
    }
 
    void removeIProgress(IProgress* iprogress){
        m_iprogressList.remove(iprogress);
    }
 
 
protected:
    virtual void onProgress(float value){
 
        List<IProgress*>::iterator itor=m_iprogressList.begin();
 
        while (itor != m_iprogressList.end() )
            (*itor)->DoProgress(value); //更新進度條
            itor++;
        }
    }
};
 
 
 
class MainForm : public Form, public IProgress
{
    TextBox* txtFilePath;
    TextBox* txtFileNumber;
 
    ProgressBar* progressBar;
 
public:
    void Button1_Click(){
 
        string filePath = txtFilePath->getText();
        int number = atoi(txtFileNumber->getText().c_str());
 
        ConsoleNotifier cn;
 
        FileSplitter splitter(filePath, number);
 
        splitter.addIProgress(this); //訂閱通知
        splitter.addIProgress(&cn); //訂閱通知
 
        splitter.split();
 
        splitter.removeIProgress(this);
 
    }
 
    virtual void DoProgress(float value){
        progressBar->setValue(value);
    }
};
 
class ConsoleNotifier : public IProgress {
public:
    virtual void DoProgress(float value){
        cout << ".";
    }
};


Observer相當於Iprogress,update相當於Doprogress。上面的程序將subject與concretesubject兩個合併。

 

觀察者模式要點總結

  • 使用面向對象的抽象,Observer模式使得我們可以獨立地改變目標與觀察者,從而使二者之間的依賴關係達到鬆耦合。
  • 目標發送通知時,無需制定觀察者,通知(可以攜帶通知信息作爲參數)會自動傳播
  • 觀察者自己決定是否需要訂閱通知,目標對象對此一無所知
  • Observer模式是基於事件的UI框架中非常常用的設計模式,也是MVC模式的一個重要組成部分。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章