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

策略模式(C++)

定义

定义一系列算法,把它们一个个封装起来,并且使它们可互相替换((变化)。该模式使得算法可独立手使用它的客户程序稳定)而变化(扩展,子类化)。
——《设计模式》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.lryc.cn/news/115686.html

相关文章:

  • 【每日一题Day290】LC1281整数的各位积和之差 | 模拟
  • 揭示CTGAN的潜力:利用生成AI进行合成数据
  • GitHub中readme.md文件的编辑和使用
  • Python 四舍五入到最接近的十位
  • Unity限制在一个范围内移动
  • dji uav建图导航系列(一)建图
  • AAAI论文阅读
  • 填补5G物联一张网,美格智能快速推进RedCap商用落地
  • 服务器杂七杂八的知识/常识归纳(不断更新)
  • 掌握Java排序算法:实现主流排序方法与性能对比
  • jdk17 SpringBoot JPA集成多数据库
  • vue 新学习 06 js的prototype ,export暴露,vue组件,一个重要的内置关系
  • 冠达管理:“高温超导”不是“室温超导”,5天4板百利电气再次澄清
  • CS 144 Lab Four 收尾 -- 网络交互全流程解析
  • Linux面试专题
  • MySQL错误日志(Error Log)详解
  • Qt应用开发(基础篇)——LCD数值类 QLCDNumber
  • 新版百度、百家号旋转验证码识别
  • PMP考试每日一练(8月8日)
  • 机器学习实战1-kNN最近邻算法
  • 【eNSP】静态路由
  • 算法训练Day42|1049. 最后一块石头的重量 II ● 494. 目标和 ● 474.一和零
  • HBase-组成
  • 第一部分:领域中的基本概念
  • react使用ref调用子组件的方法
  • JVM面试突击班2
  • 【80天学习完《深入理解计算机系统》】第二天 2.2 整数的表示【有符号数,无符号数,符号数的扩展,有无符号数的转变】
  • 基于 CentOS 7 构建 LVS-DR 群集以及配置nginx负载均衡
  • golang trace view 视图详解
  • zju代码题:4-6