<一>Observer 观察者模式

动机

在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系” 一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。如果这样的依赖关系过于紧密,将使软件不能很好地抵御变化。使用面向对象技术,可以将这种依赖关系弱化,并形成一种稳定
的依赖关系。从而实现软件体系结构的松耦合。

模式定义

定义对象间的一种一对多(变化)的依赖关系,以便当一个对象(Subject)的状态发生改变时,所有依赖于它的对象都得到通知并自动更新

以 下载过程发出进度通知 为例

//版本一
class FileDownloader
{
	string m_filePath;
	int m_fileNumber;
	shared_ptr<ProgressBar> m_progressBar;

public:
	FileDownloader(const string& filePath, 
	int fileNumber, 
	shared_ptr<ProgressBar> progressBar) :
		m_filePath(filePath), 
		m_fileNumber(fileNumber),
		m_progressBar(progressBar)
	{

	}
	void download(){

		//1.网络下载准备

		//2.文件流处理

		//3.设置进度条
		for (int i = 0; i < m_fileNumber; i++){
			//...
			float progressValue = m_fileNumber;
			progressValue = (i + 1) / progressValue;
			m_progressBar->setValue(progressValue);
		}

		//4.文件存储

	}
};


int main()
{
  //详细代码省略
  FileDownloader fd( , ,);
  fd.download();

}//end main

在上面的代码问题,在FileDownload代码中带着界面层代码

//----------------------------------------

版本二
class IProgress{
public:
	virtual void DoProgress(float value)=0;
	virtual ~IProgress(){}
};

class FileDownloader
{
	string m_filePath;
	int m_fileNumber;
	shared_ptr<IProgress>  m_iprogress; 
	
public:
	FileDownloader(const string& filePath, int fileNumber) :
		m_filePath(filePath), 
		m_fileNumber(fileNumber){

	}

	void download(){
		//1.下载动作

		//2.设置进度
		for (int i = 0; i < m_fileNumber; i++){
			//...
			float progressValue = m_fileNumber;
			progressValue = (i + 1) / progressValue;
			m_iprogress->DoProgress(value);
		}
	}

	void setProgress(shared_ptr<IProgress>  progress)
	{
		m_iprogress=progress;
	}
};

class MainForm : public Form, public IProgress
{
	shared_ptr<TextBox> txtFilePath;
	shared_ptr<TextBox> txtFileNumber;
	shared_ptr<ProgressBar> progressBar;

public:
	void Button1_Click(){

		string filePath = txtFilePath->getText();
		int number = atoi(txtFileNumber->getText().c_str());


		FileDownloader downloader(filePath, number);


		shared_ptr<IProgress> ip=make_shared<ConsoleNotifier>();
		
		//shared_ptr<IProgress> ip=shared_ptr<IProgress>(this);
		//shared_from_this(this);
		
		downloader.setProgress(ip); 


		downloader.download();
	}

	void DoProgress(float value) override {
		progressBar->setValue(value);
	}
};

class ConsoleNotifier : public IProgress {
public:
	void DoProgress(float value) override{
		cout << ".";
	}
};

class DisplayNotifier : public IProgress {
public:
	void DoProgress(float value) override{
		cout << value;
	}
};


//----------------------------------------

版本三
class IProgress{
public:
	virtual void DoProgress(float value)=0;
	virtual ~IProgress(){}
};


class FileDownloader
{
	string m_filePath;
	int m_fileNumber;

	list<shared_ptr<IProgress>>  m_iprogressList; 
	
public:
	FileDownloader(const string& filePath, int fileNumber) :
		m_filePath(filePath), 
		m_fileNumber(fileNumber){

	}

	void download(){
		//1.下载动作

		//2.设置进度
		for (int i = 0; i < m_fileNumber; i++){
			//...

			float progressValue = m_fileNumber;
			progressValue = (i + 1) / progressValue;
			onProgress(progressValue);
		}

	}

	void addIProgress(const shared_ptr<IProgress>& iprogress){
		m_iprogressList.push_back(iprogress);
	}

	void removeIProgress(const shared_ptr<IProgress>& iprogress){
		m_iprogressList.remove(iprogress);
	}
protected:
	void onProgress(float value)  
	{
		for (auto& progress : m_iprogressList){
			progress->DoProgress(value);
		}
	}
};

class MainForm : public Form, public IProgress
{
	shared_ptr<TextBox> txtFilePath;
	shared_ptr<TextBox> txtFileNumber;
	shared_ptr<ProgressBar> progressBar;

public:
	void Button1_Click(){

		string filePath = txtFilePath->getText();
		int number = atoi(txtFileNumber->getText().c_str());

		shared_ptr<IProgress> ip=make_shared<ConsoleNotifier>();

		FileDownloader downloader(filePath, number);

		downloader.addIProgress(ip); 

		downloader.download();

		downloader.removeIProgress(ip);

	}

	void DoProgress(float value) override{
		progressBar->setValue(value);
	}
};

class ConsoleNotifier : public IProgress {
public:
	void DoProgress(float value) override {
		cout << ".";
	}
};

//大多情况,到版本三已经OK了

//----------------------------------------
版本4

class IProgress{
public:
	virtual void DoProgress(float value)=0;
	virtual ~IProgress(){}
};


class Subject
{
private:
	list<shared_ptr<IProgress>>  m_iprogressList;//共享引用

	//list<weak_ptr<IProgress>> m_iprogressList;//弱引用

public:
	void addIProgress(const shared_ptr<IProgress>& iprogress){
		m_iprogressList.push_back(iprogress);
	}
	void removeIProgress(const shared_ptr<IProgress>& iprogress){
		m_iprogressList.remove(iprogress);
	}

protected:
	virtual void onProgress(float value)  
	{
		for (auto& progress : m_iprogressList){
			progress->DoProgress(value);



			/* 弱引用检查生命周期是否存在?
			auto s_progress=progress.lock();
			if ( s_progress ) {
                s_progress->notify();
            }*/ 
		}
	}
};


class FileDownloader: public Subject
{
	string m_filePath;
	int m_fileNumber;

public:
	FileDownloader(const string& filePath, int fileNumber) :
		m_filePath(filePath), 
		m_fileNumber(fileNumber){

	}
	void download(){
		//1.下载动作

		//2.设置进度
		for (int i = 0; i < m_fileNumber; i++){
			//...
			float progressValue = m_fileNumber;
			progressValue = (i + 1) / progressValue;
			Subject::onProgress(progressValue);
		}

	}
};

class MainForm : public Form, public IProgress
{
	shared_ptr<TextBox> txtFilePath;
	shared_ptr<TextBox> txtFileNumber;
	shared_ptr<ProgressBar> progressBar;

public:
	void Button1_Click(){

		string filePath = txtFilePath->getText();
		int number = atoi(txtFileNumber->getText().c_str());

		shared_ptr<IProgress> ip=make_shared<ConsoleNotifier>();


		FileDownloader downloader(filePath, number);

		downloader.addIProgress(ip); 

		downloader.download();

		splitter.removeIProgress(ip);

	}
	void DoProgress(float value) override {
		progressBar->setValue(value);
	}
};

class ConsoleNotifier : public IProgress {
public:
	void DoProgress(float value) override{
		cout << ".";
	}
};

要点总结

1: 使用面向对象的抽象,Observer模式使得我们可以独立地改变目标与观察者,从而使二者之间的依赖关系达致松耦合。
2: 目标发送通知时,无需指定观察者,通知(可以携带通知信息作为参数)会自动传播。
3: 观察者自己决定是否需要订阅通知,目标对象对此一无所知。
4: Observer模式是基于事件的UI框架中非常常用的设计模式,也是MVC模式的一个重要组成部分。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值