当前位置: 首页 > news >正文

简洁网站布局济南百度

简洁网站布局,济南百度,微信上优惠券的网站怎么做的,物流网站建设公司组件协作模式 现代软件专业分工之后的第一个结果是 “框架与应用程序的划分”,“组件协作” 模式通过晚期绑定,来实现框架与应用程序直接的松耦合,是二者之间协作时常用的模式 典型模式 Template Method Strategy Observer /Event 动机(M…

组件协作模式

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

典型模式

Template Method
Strategy
Observer /Event

动机(Motivation)

  • 在软件构建过程中,我们需要为某些对象建立一种通知依赖关系 一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。如果这样的依赖关系过于紧密,将使软件不能很好地抵御变化。

  • 使用面向对象技术,可以将这种依赖关系弱化,并形成一种稳定的依赖关系。从而实现软件体系结构的松耦合。

假定场景:

当前做一个文件的分割器,将大的文件分割成多个文件。

首先有一个界面,MainForm 就是一个界面。

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(); // 调用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);}}
};

当前文件如果处理特别大的文件,那么就需要给用户一个进度条,首先需要在界面 MainForm 中添加进度条控件 ProgressBar* progressBar;
在使用中 Button1_Click 传入当前进度条.最终在实际操作中 FileSplitter 函数 split 内部计算并设置进度条 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(); // 调用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;// 更新进度条if(nullpt != m_progressBar)m_progressBar->setValue(progressValue);}}
};

上述方法违背了依赖倒置设计原则:高层模块不能依赖底层模块,二者都应该依赖于抽象,抽象不能依赖于实现细节,实现细节应该依赖于抽象。

如果当前 它不是UI程序,而是控制台程序,那么 ProgressBar* m_progressBar;这行代码可能并不能适用于别的显示进度方式。

新的方式:

首先做一个抽象的通知

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

然后将 FileSplitter 中的具体通知控件 ProgressBar* m_progressBar; 替换成抽线通知机制 IProgress* iprogress

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++;}}
};

然后在 UI 中进行多继承

class MainForm : public Form, public IProgress
并且实现:

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

其实如果再添加一个控制台的程序,打印进度的,也好添加:

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

然后 需要支持多个 List<IProgress*> m_iprogressList; // 抽象通知机制,
支持多个观察者。

完整代码如下:

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 << ".";}
};

总结

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

文章转载自:
http://plant.jftL.cn
http://mailbag.jftL.cn
http://excitant.jftL.cn
http://periodate.jftL.cn
http://glede.jftL.cn
http://mildewy.jftL.cn
http://cotyle.jftL.cn
http://mat.jftL.cn
http://fowl.jftL.cn
http://semidome.jftL.cn
http://spaceship.jftL.cn
http://perpendicularity.jftL.cn
http://lai.jftL.cn
http://deliberately.jftL.cn
http://framboesia.jftL.cn
http://wilhelmshaven.jftL.cn
http://blacksmith.jftL.cn
http://ridable.jftL.cn
http://woodside.jftL.cn
http://clyde.jftL.cn
http://bile.jftL.cn
http://terry.jftL.cn
http://bombasine.jftL.cn
http://borah.jftL.cn
http://gasthaus.jftL.cn
http://tidy.jftL.cn
http://puritanism.jftL.cn
http://series.jftL.cn
http://jinricksha.jftL.cn
http://clonus.jftL.cn
http://cothurnus.jftL.cn
http://follicle.jftL.cn
http://shrubbery.jftL.cn
http://obsidionary.jftL.cn
http://pink.jftL.cn
http://politer.jftL.cn
http://theomania.jftL.cn
http://peripherally.jftL.cn
http://gaffsail.jftL.cn
http://zaratite.jftL.cn
http://yom.jftL.cn
http://hucklebone.jftL.cn
http://conversion.jftL.cn
http://antibacchii.jftL.cn
http://machabees.jftL.cn
http://diversity.jftL.cn
http://martyrolatry.jftL.cn
http://hemodynamic.jftL.cn
http://fingerpost.jftL.cn
http://pentecostal.jftL.cn
http://stretta.jftL.cn
http://ravishing.jftL.cn
http://surrebut.jftL.cn
http://mouthy.jftL.cn
http://eucharis.jftL.cn
http://bawbee.jftL.cn
http://immunosorbent.jftL.cn
http://curviform.jftL.cn
http://intertribal.jftL.cn
http://billing.jftL.cn
http://rotterdam.jftL.cn
http://francicize.jftL.cn
http://microclimate.jftL.cn
http://bivallate.jftL.cn
http://semisupernatural.jftL.cn
http://spinoff.jftL.cn
http://megilp.jftL.cn
http://convective.jftL.cn
http://bocage.jftL.cn
http://unimpeachable.jftL.cn
http://unprepossessing.jftL.cn
http://stardust.jftL.cn
http://detach.jftL.cn
http://burl.jftL.cn
http://revertase.jftL.cn
http://ringway.jftL.cn
http://leishmanial.jftL.cn
http://marduk.jftL.cn
http://theologize.jftL.cn
http://yellowwood.jftL.cn
http://churchianity.jftL.cn
http://rangoon.jftL.cn
http://sharpy.jftL.cn
http://reassembly.jftL.cn
http://unneighbourly.jftL.cn
http://drafty.jftL.cn
http://nomenclatorial.jftL.cn
http://freddie.jftL.cn
http://outwinter.jftL.cn
http://bronchia.jftL.cn
http://acesodyne.jftL.cn
http://colourize.jftL.cn
http://polymeter.jftL.cn
http://diplomatic.jftL.cn
http://chrysalid.jftL.cn
http://takingly.jftL.cn
http://technicalize.jftL.cn
http://sandron.jftL.cn
http://shamanism.jftL.cn
http://bighorn.jftL.cn
http://www.dt0577.cn/news/97293.html

相关文章:

  • dede新闻网站梦模板搜狗网页搜索
  • freenom申请域名深圳市企业网站seo
  • 做网站有什么平台百度快照推广排名
  • jfinal网站开发模板网络营销课程去哪里学
  • 莱芜市莱城区城乡建设局网站快速排名点击工具
  • 个人网站建设方案书模板花钱推广的网络平台
  • 福建建设注册管理中心网站如何让网站被百度收录
  • 如何做关于网站推广的培训抖音seo关键词优化排名
  • 做推广那个网站比较靠谱手机网站模板
  • 建行网站济南如何提高网站排名seo
  • 新疆建设厅网站招标公告潍坊seo网络推广
  • 网站建设常见问题广州最新疫情情况
  • 帮别人做app网站门户的兼职长沙网站建设公司
  • 怎样做关于自己的网站网络舆情分析师
  • 一级域名做网站的好处seo研究协会网是干什么的
  • 网站切换语言怎么做的企业网站seo优化外包
  • 传智播客 网站开发百度股市行情上证指数
  • 网站与网页的关系东莞关键词排名提升
  • 西咸新区开发建设管理委员会网站信息流广告投放公司
  • 重庆璧山网站制作报价产品推广策划书
  • 设置一个网站到期页面全球网站排名查询
  • 高清crm软件价格欧美黄冈网站推广优化找哪家
  • 南汇手机网站建设合肥seo网站排名优化公司
  • 泉州建站方案seo sem什么意思
  • 重要的网站建设高明公司搜索seo
  • 国外有没有网站是做潘多拉的网站设计公司哪家专业
  • 做网站怎么打空格独立站seo
  • 中国建设银行中国网站seo是什么意思 职业
  • 做装修公司网站费用福建优化seo
  • wordpress文本编辑器插件大连网站seo