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

2.策略模式

UML图

在这里插入图片描述

代码

main.cpp

#include "Strategy.h"
#include "Context.h"void test()
{Context* pContext = nullptr;/* StrategyA */pContext = new Context(new StrategyA());pContext->contextInterface();/* StrategyB */pContext = new Context(new StrategyB());pContext->contextInterface();/* StrategyC */pContext = new Context(new StrategyC());pContext->contextInterface();delete pContext;pContext = nullptr;
}int main()
{test();system("pause");
}

Strategy.h

#pragma once
#include <iostream>
using namespace std;/*
策略模式:定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的用户。
*//* 抽象策略基类:定义所有支持算法的公共接口 */
class Strategy
{
public:virtual void algorithmInterface() = 0;
};/* 策略A */
class StrategyA :public Strategy
{
public:virtual void algorithmInterface()override{cout << "算法A实现" << endl;}
};/* 策略B */
class StrategyB :public Strategy
{
public:virtual void algorithmInterface()override{cout << "算法B实现" << endl;}
};/* 策略C */
class StrategyC :public Strategy
{
public:virtual void algorithmInterface()override{cout << "算法C实现" << endl;}
};

Context.h

#pragma once
#include "Strategy.h"/*
笔者感受:使用类C操控类A继承体系A1,A2,A3中公有对外暴露接口。
利用一个额外的类,(1)将strategy抽象策略基类,作为额外类的入参;
(2)将strategy抽象策略基类作为额外类的成员变量,利用多态原理,接收外面传来的具体抽象策略。
(3)对外暴露的接口中,使用成员变量抽象策略基类,调用策略继承体系中的子类们都需要继承的纯虚函数接口。
即可,使用额外类操控策略继承体系中公有对外暴露的接口了。
*/
class Context
{
public:Context(Strategy* pStrategy) :m_pStrategy(pStrategy) {}void contextInterface();
private:Strategy* m_pStrategy{ nullptr };
};void Context::contextInterface()
{m_pStrategy->algorithmInterface();
}

策略模式+简单工厂

  • 优点:策略模式+简单工厂:可以完全将策略继承体系与用户端完全剥离开来,将策略继承体系完全封装起来,对用户完全不可见。
  • 总结
    • 类C通过没什么信息含量的枚举作为入参,利用简单工厂生成类A继承体系中的各子类A1、A2、A3。同时,用基类A作为类C的成员变量,接一下刚生成的类A的子类。
    • 类C对外统一暴露一个接口,该接口中,类C的成员变量类A调用继承体系公有对外暴露的接口func()。

main.cpp

#include "StrategyFactory.h"/*
策略方法+简单工厂:可以将策略继承体系完全剥离开来,完全封装起来,对用户完全不可见。
*/void test()
{StrategyFactory* pStrategyFactory = nullptr;/* StrategyA */pStrategyFactory = new StrategyFactory(StrategyType::eStrategyA);pStrategyFactory->contextInterface();/* StrategyB */pStrategyFactory = new StrategyFactory(StrategyType::eStrategyB);pStrategyFactory->contextInterface();/* StrategyC */pStrategyFactory = new StrategyFactory(StrategyType::eStrategyC);pStrategyFactory->contextInterface();delete pStrategyFactory;pStrategyFactory = nullptr;
}int main()
{test();system("pause");
}

Strategy.h

#pragma once
#include <iostream>
using namespace std;/*
策略模式:定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的用户。
*//* 抽象策略基类:定义所有支持算法的公共接口 */
class Strategy
{
public:virtual void algorithmInterface() = 0;
};/* 策略A */
class StrategyA :public Strategy
{
public:virtual void algorithmInterface()override{cout << "算法A实现" << endl;}
};/* 策略B */
class StrategyB :public Strategy
{
public:virtual void algorithmInterface()override{cout << "算法B实现" << endl;}
};/* 策略C */
class StrategyC :public Strategy
{
public:virtual void algorithmInterface()override{cout << "算法C实现" << endl;}
};

StrategyFactory.h

#pragma once
#include "Strategy.h"/*
我的感受:使用类C操控类A继承体系A1,A2,A3中公有对外暴露接口。
利用一个额外的类,(1)将strategy抽象策略基类,作为额外类的入参;
(2)将strategy抽象策略基类作为额外类的成员变量,利用多态原理,接收外面传来的具体抽象策略。
(3)对外暴露的接口中,使用成员变量抽象策略基类,调用策略继承体系中的子类们都需要继承的纯虚函数接口。
即可,使用额外类操控策略继承体系中公有对外暴露的接口了。
*/
enum StrategyType
{eStrategyA,eStrategyB,eStrategyC
};class StrategyFactory
{
public:StrategyFactory(StrategyType nType);~StrategyFactory();void contextInterface();
private:Strategy* m_pStrategy{ nullptr };
};StrategyFactory::StrategyFactory(StrategyType nType)
{switch (nType){case eStrategyA:m_pStrategy = new StrategyA();break;case eStrategyB:m_pStrategy = new StrategyB();break;case eStrategyC:m_pStrategy = new StrategyC();break;}
}StrategyFactory::~StrategyFactory()
{delete m_pStrategy;m_pStrategy = nullptr;
}void StrategyFactory::contextInterface()
{m_pStrategy->algorithmInterface();
}
http://www.lryc.cn/news/172787.html

相关文章:

  • 算法通过村第七关-树(递归/二叉树遍历)黄金笔记|迭代遍历
  • MySQL数据库简介+库表管理操作+数据库用户管理
  • PyTorch实战:卷积神经网络详解+Python实现卷积神经网络Cifar10彩色图片分类
  • MapRdeuce工作原理
  • 完整指南:使用JavaScript从零开始构建中国象棋游戏
  • PG-DBA培训19:PostgreSQL高可用集群项目实战之Patroni
  • 数据库管理-第105期 安装Database Valut组件(20230919)
  • 企望制造ERP系统RCE漏洞 复现
  • 【unity小技巧】Unity 存储存档保存——PlayerPrefs、JsonUtility和MySQL数据库的使用
  • 2023-9-22 滑雪
  • 基于Yolov8的工业小目标缺陷检测(6):多检测头结合小缺陷到大缺陷一网打尽的轻量级目标检测器GiraffeDet,暴力提升工业小目标缺陷检测能力
  • exe文件运行后无输出直接闪退如何找解决办法
  • OpenHarmony应用开发—ArkUI组件集合
  • Linux(CentOS)安装msf
  • 工作几年还是悟不懂自动化测试的意义
  • Redis面试问题三什么是缓存雪崩怎么解决
  • 【Unittest】自动化测试框架核心要素
  • Hyperloglog
  • 如何自动获取短信验证码?
  • Linux 本地 Docker Registry本地镜像仓库远程连接【内网穿透】
  • 基于Yolov8的工业小目标缺陷检测(4):SPD-Conv,低分辨率图像和小物体涨点明显
  • 平均精度(AP)
  • 建议收藏《Verilog代码规范笔记_华为》(附下载)
  • Nginx环境搭建、负载均衡测试
  • 软件工程知识总结梳理
  • Mybatis自动映射Java对象 与 MySQL8后的JSON数据
  • 【JavaScript】深拷贝和浅拷贝
  • 【SLAM】10.纵观SLAM,对比方案和未来方向
  • PyTorch中DistributedDataParallel使用笔记
  • 前端面试的话术集锦第 18 篇博文——高频考点(HTTP协议 TLS协议)