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

舟山网站建设哪家好哈尔滨自助建站软件

舟山网站建设哪家好,哈尔滨自助建站软件,百度开发者搜索,百度智能导航地图定义 定义一系列算法,把它们一个个封装起来,并且使它们可互相替换((变化)。该模式使得算法可独立手使用它的客户程序稳定)而变化(扩展,子类化)。 ——《设计模式》GoF 使用场景 在软件构建过程中,某些对象使用的算法可能多种多…

定义

定义一系列算法,把它们一个个封装起来,并且使它们可互相替换((变化)。该模式使得算法可独立手使用它的客户程序稳定)而变化(扩展,子类化)。
——《设计模式》GoF

使用场景

  • 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到对象中,将会使对象变得异常复杂;而且有时候支持不使用的算法也是一个性能负担。
  • 如何在运行时根据需要透明地更改对象的算法?将算法与对象本身解耦,从而避免上述问题?

结构

在这里插入图片描述

代码示例

//Strategy.h
/****************************************************/
#ifndef STRATEGY_H
#define STRATEGY_H
#include<iostream>
using namespace std;//创建一个定义活动的Strategy的抽象接口
class Strategy
{
public:Strategy() {};virtual ~Strategy() {};virtual int doOperation(int num1, int num2)=0;
};//创建一个实体活动的OperationAdd类
class OperationAdd:Strategy
{
public:OperationAdd() {};virtual ~OperationAdd() {};int doOperation(int num1, int num2) { return num1 + num2; };
};//创建一个实体活动的OperationSubstract类
class OperationSubstract :Strategy
{
public:OperationSubstract() {};virtual ~OperationSubstract() {};int doOperation(int num1, int num2) { return num1 - num2; };
};//创建一个实体活动的OperationMultiply类
class OperationMultiply :Strategy
{
public:OperationMultiply() {};virtual ~OperationMultiply() {};int doOperation(int num1, int num2) { return num1 * num2; };
};//创建一个使用某种策略的Context类
class Context
{
public:Context(Strategy *strate) { mstrate = strate; };virtual ~Context() { delete mstrate; mstrate = NULL; };int executeStrategy(int num1, int num2) { return mstrate->doOperation(num1,num2); };
private:Strategy *mstrate;
};#endif
//test.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Strategy.h"int main()
{Context *c1 = new Context((Strategy*)new OperationAdd());Context *c2 = new Context((Strategy*)new OperationSubstract());Context *c3 = new Context((Strategy*)new OperationMultiply());cout <<"1 + 2 = "<<c1->executeStrategy(1, 2) << endl;cout <<"1 - 2 = "<< c2->executeStrategy(1, 2) << endl;cout <<"1 * 2 = "<< c3->executeStrategy(1, 2) << endl;delete c1;c1 = NULL;delete c2;c2 = NULL;delete c3;c3 = NULL;return 0;
}

运行结果:
在这里插入图片描述

要点总结

  • Strategy及其子类为组件提供了一系列可重用的算法,从而可以使得类型在运行时方便地根据需要在各个算法之间进行切换。
  • Strategy模式提供了用条件判断语句以外的另一种选择,消除条件判断语句,就是在解耦合。含有许多条件判断语句的代码通常都需要Strategy模式。
  • 如果Strategy对象没有实例变量,那么各个上下文可以共享同一个strategy对象,从而节省对象开销。
http://www.sczhlp.com/news/77216/

相关文章:

  • 菏泽城乡住房建设局网站wordpress目录图片
  • 仪征 做网站房屋装修设计网
  • 使用 Python + OpenCV + Tesseract OCR 自动识别
  • 安装用友U8的常见错误及用友下载地址
  • 学生ppt模板免费下载 素材班级优化大师app下载
  • 绵阳网站推广排名胶南网
  • 泉州城乡住房建设厅网站wordpress 多本小说
  • ppt免费下载模板网站惠州外发加工网
  • 网站建设包括哪方面知识产权网站模板
  • iis发布php网站黄骅港潮汐
  • 西餐甜点网站建设浙江建设技术职业学院网站
  • 国内环保行业网站开发公司建站费用
  • 深圳市建设交易中心网站首页吉林市百姓网免费发布信息网
  • Go 识别验证码:Tesseract OCR 的实践与优化
  • 网站icon怎么做的成立公司需要多少费用
  • 山西响应式网站建设制作PHP网站建设视频免费
  • 长春市住房和城乡建设局网站昆明优化广告公司
  • 网站维护费怎么做会计分录怎样做免费网站会员
  • 百度企业网站建设单页面网站源码
  • 上海网站建设方案网页微博打不开
  • 实验室建设网站做的网站百度排名没有图片显示
  • 论职能网站建设济南高端网站建设公司
  • 免费发布信息的网站平台有哪些美工接单网
  • 河北软件开发网站建设wordpress源码下载
  • 杭州电子商务网站建设公司网站的根目录
  • 山西住房城乡建设厅网站网站建设大图
  • phpstudy做网站wordpress添加下文件
  • 微软勒索病毒补丁(永恒之蓝漏洞补丁)汇总XP/2003/2008/Win7/Win8/Win10
  • python学习路线
  • 做商城网站的流程介绍建设网站的HTML代码