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

一、定义:

策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。

策略模式让算法独立于使用它的客户而独立变化。

二,核心

抽象策略(抽象基类)(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;
}
相关推荐
我是陈泽9 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
Mr.Z.41116 分钟前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
优雅的小武先生19 分钟前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
Death20020 分钟前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
虽千万人 吾往矣25 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
创作小达人28 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
郭二哈31 分钟前
C++——list
开发语言·c++·list
杨荧32 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
ZPC821038 分钟前
Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)
开发语言·python·matplotlib
镜花照无眠40 分钟前
Python爬虫使用实例-mdrama
开发语言·爬虫·python