设计模式第五讲--Observer 观察者模式
1. 现代软件专业分工之后的第一个结果是“框架与应用程序的划分”,“组件协作”模式通过晚绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的模式。
典型模式
Template Method
Strategy
Oberver/Event
2. 动机
在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系”——一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。如果这样的依赖关系过于紧密,将使软件不能很好的抵御变化。
使用面向对象技术,可以使这种依赖关系弱化,并形成一种稳定的依赖关系。从而实现软件体系结构的松耦合。
3.代码
MainForm.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<string> #include "FileSplitter.cpp" using namespace std; class MainForm : public Form { TextBox* txtFilePath; TextBox* txtFileNumber; public : void Button1_Click() { string filePath = txtFilePath->getText(); int number = atoi (txtFileNumber->getText().c_str()); FileSplitter splitter(filePath, number); splitter.split(); } }; |
FileSplitter.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<string> using namespace std; class FileSplitter { string m_filePath; int m_fileNumber; 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) { //... } } }; |
现在有一个需求是要显示进度信息
一般代码修改如下
MainForm1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<string> #include "FileSplitter1.cpp" using namespace std; 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(); } }; |
Filesplitter1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<string> using namespace std; 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) { //... if (m_progressBar != nullptr ) { m_progressBar->setValue((i + 1) / m_fileNumber); //更新进度条 } } } }; |
这种修改方式造成了显示进度的方式与FileSplitter类形成紧耦合,违背了依赖倒置原则。
4. 使用观察者模式修改后的代码
MainForm2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<string> #include "FileSplitter3.cpp" using namespace std; 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()); FileSplitter splitter(filePath, number, this ); splitter.split(); } virtual void DoProgress( float value) { progressBar->setValue(value); } }; |
FileSplitter2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include<string> #include<vector> #include<list> using namespace std; class IProgress { public : virtual void DoProgress( float value) = 0; virtual ~IProgress() {} }; class FileSplitter { string m_filePath; int m_fileNumber; IProgress* m_iprogress; public : FileSplitter( const string& filePath, int fileNumber, IProgress* iprogress) : m_filePath(filePath), m_fileNumber(fileNumber), m_iprogress(iprogress) { } void split() { //1. 读取大文件 //2.分批次向小文件中写入 for ( int i = 0; i < m_fileNumber; ++i) { //... float progressValue = m_fileNumber; progressValue = (i + 1) / progressValue; onProgress(progressValue); } } protected : void onProgress( float value) { if (m_iprogress != nullptr ) { m_iprogress->DoProgress(value); } } }; |
以上的代码使用了观察者模式,但是只是针对ProgressBar这一种观察者,实际上可以有多个观察者。再次优化后的代码如下:
MainForm3.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include<string> #include "FileSplitter2.cpp" using namespace std; 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.add_IProgress( this ); splitter.add_IProgress(&cn); splitter.split(); } virtual void DoProgress( float value) { progressBar->setValue(value); } }; class ConsoleNotifier: public IProgress //当发生变化时,所有继承IProgress的类都会街道通知。 { public : virtual void DoProgress( float value) { cout << "." ; } }; |
FileSplitter3.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<string> #include<vector> #include<list> using namespace std; class IProgress { public : virtual void DoProgress( float value) = 0; virtual ~IProgress(){} }; class FileSplitter { string m_filePath; int m_fileNumber; list<IProgress*> m_iprogress; //可以通知多个观察者 public : FileSplitter( const string& filePath, int fileNumber) : m_filePath(filePath), m_fileNumber(fileNumber) { } void add_IProgress(IProgress* iprogress) { m_iprogress.push_back(iprogress); } void remove_IProgress(IProgress* iprogress) { m_iprogress. remove (iprogress); } void split() { //1. 读取大文件 //2.分批次向小文件中写入 for ( int i = 0; i < m_fileNumber; ++i) { //... float progressValue = m_fileNumber; progressValue = (i + 1) / progressValue; onProgress(progressValue); } } protected : void onProgress( float value) { for ( auto ite: m_iprogress) ite->DoProgress(value); //更新进度条 } }; |
5. 模式定义
定义对象间的一种一对多(变化)的依赖关系,以便当一个对象(Subject)的状态发生改变时,所有依赖于它的对象都得到通知并自动更新。
6. 结构
7. 要点总结
使用面向对象的抽象,Observer模式使得我们可以独立的改变目标与观察者,从而使二者之间的依赖关系达至松耦合。
目标发送通知时,无需指定观察着,通知(可以携带通知信息作为参数)会自动传播。
观察者自己决定是否需要订阅通知,目标对象对此一无所知。
Observer模式是基于事件的UI框架中非常常用的设计模式,也是MVC模式的一个重要组成部分。
posted on 2021-12-06 21:38 xcxfury001 阅读(30) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用