一、概念
定義對象間的一種一對多(變化)的依賴關係,以便當一個對象(Subject)的狀態發生改變時,所有依賴於它的對象都得到通知並自動更新。——《設計模式》GOF
二、動機
在軟件構建過程中,我們需要爲某些對象建立一種“通知依賴關係”,一個對象(目標對象)的狀態發生改變,所有的依賴對象(觀察者對象)都將得到通知。如果這樣的依賴關係過於緊密,將使得軟件不能很好的抵禦變化。
使用面向對象技術,可以將這種依賴關係弱化,並形成一種穩定的依賴關係,從而實現軟件體系結構的鬆耦合。
三、源代碼講解
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,讓該類去分割文件
//將進度條傳入文件分割類,在文件分割時改變進度條數據
FileSplitter splitter(filePath, number, progressBar);
//進行分割
splitter.split();
}
};
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 IProgress {
public:
virtual void DoProgress(float value) = 0; // 0-1一個進度值
virtual ~IProgress() { }
};
// 中層模塊
class FileSplitter {
string m_filePath;
int m_fileNumber;
List<IProgress*> m_iprogressList; // 抽象通知機制,支持多個觀察者,實現具體到抽象的躍遷
// 上面實現了從緊耦合變爲鬆耦合,FileSplitter沒有再耦合一個具體細節(界面類)
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); // 向所有觀察者發送通知
}
}
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++;
}
}
};
// C++不推薦多繼承,因爲他會帶來很多耦合性問題,但是支持一種,第一個是主繼承,其他都是接口或者抽象基類
class MainForm : public Form, public IProgress {
TextBox* txtFilePath;
TextBox* txtFileNumber;
// 這裏面使用progressBar是沒有關係的,他和MainForm本身是一體的,緊耦合的
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) {
// 主界面選擇的GUI進度條,我們可以添加,也可以添加addIProgress(this)
progressBar->setValue(value);
}
};
class ConsoleNotifier : public IProgress {
public:
// 具體的通知機制
virtual void DoProgress(float value) {
cout << ".";
}
};
五、類圖結構
六、要點總結
-
使用面向對象的抽象,Observer模式使得我們可以獨立地改變目標與觀察者,從而使二者之間的依賴關係達到鬆耦合。
-
目標發送通知時,無需指定觀察者,通知(可以攜帶通知信息作爲參數)會自動傳播。
-
觀察者自己決定是否需要訂閱通知,目標對象對此一無所知。
-
Observer模式是基於事件的UI框架中非常常用(和Template一樣常用)的設計模式,也是MVC模式中一個重要組成部分。