zoukankan      html  css  js  c++  java
  • 五:Observer 观察者模式

       组件协作模式:现代软件专业分工之后的第一个结果是框架与应用程序的划分组件协作模式通过晚期绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的模式。

    (1)动机(Motivation

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

    (2)定义

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

    (3)要点总结

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

    重构示例代码:
    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);//发送通知
            }
    
        }
    
    
        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 << ".";
        }
    };
  • 相关阅读:
    Outlook 2003 最小化到系统托盘方法 [转]
    Sql Server 得到当月第一天
    禁止用户对系统数据库表的SELECT权限
    解决IE二级链接无法打开故障
    服务器安全设置全攻略
    使用TSQL脚本在SQL Server创建角色,并给角色赋予相应权限
    Redis内部阻塞式操作有哪些?
    UML和OO
    PetShop 4 详解(转载)
    Blog开通了
  • 原文地址:https://www.cnblogs.com/love-life-insist/p/12904963.html
Copyright © 2011-2022 走看看