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

工业信息化部网站备查询建邺做网站价格

工业信息化部网站备查询,建邺做网站价格,wordpress 后台开发,建立网站的是什么人定义 定义一系列算法,把它们一个个封装起来,并且使它们可互相替换((变化)。该模式使得算法可独立手使用它的客户程序稳定)而变化(扩展,子类化)。 ——《设计模式》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.yayakq.cn/news/464883/

相关文章:

  • 72搭建网站网页婚纱摄影结婚网
  • 动效h5网站莱芜网站推广
  • 网站的开发是使用什么技术wordpress 关于我们页面模板
  • 用vultr做网站wordpress 自定义页眉
  • 长春火车站出站要求广州手机软件开发定制
  • 做网站哪个便宜茂名建网站
  • 爱站网app旅游网站建设步骤
  • vue 做电商网站化妆品可做的团购网站有哪些
  • 网站开发手册正规手机网站建设平台
  • 学校网站建设 效果百度应用商店
  • 大网站开发费用怎么注册公司企业邮箱
  • 淄博周村网站建设方案上海网页建站
  • 西部数码网站助手教程企业营销平台
  • 门户网站 开发语言wordpress没有权限设置
  • 七台河做网站个人网站建设营销推广
  • 如何做期货培训网站办公室装修效果图简约大气
  • 公司网站有收录没排名wordpress设置标题
  • 怎么做提货网站电子商务网站体系结构有哪些
  • wordpress 没有注册济南网站建设yigeseo
  • 做网站要学哪些找别人做网站多少钱
  • 深圳市住房建设部网站在线取公司名字 免费
  • 街道网站建设优化就是开除吗
  • 池州哪里做网站网站图片导入wordpress
  • 新鸿儒网站网站建设外包被骗
  • 网站设计与建设代码asp网站 上传空间
  • 网址域名大全安徽网站关键字优化
  • 企业如何进行网络推广北京网站关键词优化
  • 网站备案最新备案号手机设计图纸软件
  • 公司网站建设的好处杭州高端网站设计公司
  • 全球电子商务网站小程序官网登录入口