c++的策略模式,就是多态

news/2024/10/11 9:24:40/

一、定义:
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。
策略模式让算法独立于使用它的客户而独立变化。
二,核心

抽象策略(抽象基类)(Strategy): 抽象策略类。
具体策略(具体子类)(ConcreteStrategy):封装了继续相关的算法和行为。
环境角色(Context):持有一个策略类的引用,最终给客户端调用。

三,UML类图
在这里插入图片描述
用法示例:

1.空调支持3种模式。冷风模式(coldWind), 热风模式(hotWind),无风模式(noWind)。
1.1当选择coldWind模式,将输送冷风;
1.2当选择hotWind模式,将输送热风;
1.3在选择noWind模式时,空调什么都不做。这里coldWind, hotWind, noWind 其实就是ConcreteStrategy。 myStrategy 是抽象策略类。 所以我们开始这么封装我们策略类

myStrategy.h

#pragma once
#include <iostream>
using namespace std;// 抽象策略角色(Strategy)
//抽象基类写了两个虚函数
class myStrategy
{
public:		myStrategy() { std::cout << "new myStrategy" << endl; };virtual ~myStrategy() { std::cout << "delete myStrategy!" << endl; };virtual void blowWind() = 0;
};//具体策略角色(ConcreteStrategy)
class coldWind: public myStrategy
{
public:coldWind() { std::cout << "new coldWind" << endl; };~coldWind() { std::cout << "delete clod wind!" << endl; };void blowWind(){std::cout << "Blowing clod wind!" << endl;}
};//具体策略角色(ConcreteStrategy)
//继承基类实现热风
class hotWind: public myStrategy
{
public:hotWind() { std::cout << "new hotWind" << endl; };~hotWind() { std::cout << "delete hot wind!" << endl; };void blowWind(){std::cout << "Blowing hot wind!" << endl;}
};
//具体策略角色(ConcreteStrategy)
//继承基类实现无风
class noWind: public myStrategy
{
public:noWind() { std::cout << "new noWind" << endl; };~noWind() { std::cout << "delete no wind!" << endl; };void blowWind(){std::cout << "Blowing no wind!" << endl;}
};

context.h
使用基类指针调用子类的方法

#pragma once//环境角色(Context)
#include "myStrategy.h"class windMode
{
public:windMode(myStrategy* wind);~windMode();void blowWind();void freePtr();	
private://环境角色持有的策略类指针(或引用)myStrategy* m_wind;			
};

context.cpp

#include "context.h"windMode::windMode(myStrategy* wind): m_wind(wind)
{
}windMode::~windMode()
{
}void windMode::blowWind()
{m_wind->blowWind();
}void windMode::freePtr()
{if (m_wind){std::cout << "delete memory" << endl;delete m_wind;m_wind = NULL;}
}

用基类指针调用子类方法
main.cpp

#include <iostream>
#include "windMode.h"
using namespace std;
int main()
{//策略模式使用windMode* hot_Wind = new windMode(new hotWind());windMode* cold_Wind = new windMode(new coldWind());windMode* no_Wind = new windMode(new noWind());hot_Wind->blowWind();cold_Wind->blowWind();no_Wind->blowWind();hot_Wind->freePtr();cold_Wind->freePtr();no_Wind->freePtr();return 0;
}

http://www.ppmy.cn/news/1443499.html

相关文章

sql将日期区间拆分为多行

将日期区间拆分为多行 将二维表格中的开始时间结束间用sql拆分成多行连续的时间 源数据 start_dateend_date2023-12-012023-12-03 结果 biz_datestart_dateend_date2023-01-012023-12-012023-12-032023-01-022023-12-012023-12-032023-01-032023-12-012023-12-03 代码 S…

GO语言核心30讲 进阶技术

原站地址&#xff1a;Go语言核心36讲_Golang_Go语言-极客时间 一、数组和切片 1. 两者最大的不同&#xff1a;数组的长度是固定的&#xff0c;而切片的长度是可变的。 2. 可以把切片看成是对数组的一层封装&#xff0c;因为每个切片的底层数据结构中&#xff0c;一定会包含一…

服务器网站漏洞怎么修复

服务器网站漏洞的修复是一个关键且复杂的过程&#xff0c;涉及到多个层面的安全加固。以下是一个关于如何修复服务器网站漏洞的详细指南。安全狗专业做服务器安全&#xff0c;有任何服务器安全问题都可以找安全狗哦. ​一、识别和分析漏洞 首先&#xff0c;要确定服务器网站存在…

[C++] 类和对象 _ 剖析构造、析构与拷贝

一、构造函数 构造函数是特殊的成员函数&#xff0c;它在创建对象时自动调用。其主要作用是初始化对象的成员变量&#xff08;不是开辟空间&#xff09;。构造函数的名字必须与类名相同&#xff0c;且没有返回类型&#xff08;即使是void也不行&#xff09;。 在C中&#xff0…

CSS border边框(理解网页边框制作)

目录 一、border边框介绍 1.概念 2.特点 3.功能 4.应用 二、border边框用法 1.border边框属性 2.边框样式 3.边框宽度 4.边框颜色 5.边框-单独设置各边 6.边框-简写属性 三、border边框属性 四、border边框实例 1.创建带有阴影效果的边框&#xff1a; 2. 创建一个类似标…

Llama 3消费级PC安装与运行教程

我是 Llama 的忠实粉丝。 Meta 发布其 LLM 开源代码对整个科技界来说是一项净收益&#xff0c;其宽松的许可证允许大多数中小型企业在几乎没有任何限制的情况下使用其 LLM&#xff08;当然&#xff0c;在法律范围内&#xff09;。 他们的最新版本是备受期待的 Llama 3。 Llama…

Golang Colly批量爬取小红书图片

语言:Golang 库:Iris/Colly 先看输入日志: Saved file: images\20240428190531_2_0.jpg It is image 20240428190532_2_1.jpg Saved file: images\20240428190532_2_1.jpg It is image 20240428190533_2_2.jpg Saved file: images\20240428190533_2_2.jpg It is image 2024…

香港BTC、ETH现货ETF同时通过,对行业意义几何?

香港比美国更快一步通过以太坊现货 ETF。 2024 年 4 月 15 日&#xff0c;香港嘉实国际资产管理有限公司&#xff08;Harvest Global Investments&#xff09;今天宣布&#xff0c;得到香港证监会的原则上批准&#xff0c;将推出两大数字资产&#xff08;比特币及以太坊&#…