c++ 策略模式

一、定义:

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

1.1 角色:
抽象策略角色(Strategy): 抽象策略类。

具体策略角色(ConcreteStrategy):封装了继续相关的算法和行为。

环境角色(Context):持有一个策略类的引用,最终给客户端调用。
1.2 UML图:

在这里插入图片描述

二、示例

2.1 项目结构

在这里插入图片描述

2.2 .h .cpp文件

myStrategy.h
注:里面的具体策略角色,可以单独写成一个类文件。


/*
如现在你是一个设计师,你正在设计一种空调。但是你们的空调要支持3种模式。冷风模式(coldWind), 热风模式(hotWind),无风模式(noWind)。
当选择coldWind模式,将输送冷风;当选择hotWind模式,将输送热风;在选择noWind模式时,空调什么都不做。你将考虑如何为空调设计应用程序?如果将来空调需要增加支持新的模式呢?

这道面试题,其实可以用各种模式实现,然而在这里我理解策略模式比较合适。我们将冷风模式,和热风模式以及无风模式可以理解为各种不同的算法。显然策略模式非常符合。

这里coldWind, hotWind, noWind 其实就是ConcreteStrategy。 myStrategy 是抽象策略类。 所以我们开始这么封装我们策略类
*/
#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;
	}
};

windMode.h

#pragma once

//环境角色(Context)

#include "myStrategy.h"

class windMode
{
public:
	windMode(myStrategy* wind);

	~windMode();

	void blowWind();
	void freePtr();	
private:
	myStrategy* m_wind;				//环境角色持有的策略类指针(或引用)
};


windMode.cpp

#include "windMode.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();

	std::cout << "\nhello world" << endl;
	return 0;
}

三、运行结果

在这里插入图片描述

四、策略模式与简单工厂模式的区别

工厂模式和策略模式看着很像,经常让人混淆不清;
它们的区别在哪里,需要细细体味;

4.1 相似点

在模式结构上,两者很相似;

4.2 差异

4.2.1 用途不一样
工厂是创建型模式,它的作用就是创建对象;
策略是行为型模式,它的作用是让一个对象在许多行为中选择一种行为;

4.2.2 关注点不一样

	一个关注对象创建
	一个关注行为的封装

4.2.3 解决不同的问题

工厂模式是创建型的设计模式,它接受指令,创建出符合要求的实例;它主要解决的是资源的统一分发,将对象的创建完全独立出来,让对象的创建和具体的使用客户无关。主要应用在多数据库选择,类库文件加载等。
策略模式是为了解决的是策略的切换与扩展,更简洁的说是定义策略族,分别封装起来,让他们之间可以相互替换,策略模式让策略的变化独立于使用策略的客户。

4.2.4 工厂相当于黑盒子,策略相当于白盒子;

4.3 举例说明
工厂模式

有一天你决定去吃培根披萨,首先得选择店铺,A店和B店都有培根披萨;
你点了A店的培根披萨,过了二十分钟,你的披萨就来了就可以吃到了。但这个披萨是怎么做的,到底面粉放了多少,培根放了多少,佐料放了多少,有多少道工序,你是不需要管的,你需要的是一个美味培根披萨。

策略模式

在披萨店,你要一个培根披萨,老板说有标准的pizza,也可以自己去做。原料有培根、面粉、佐料。工序有1、2、3工序,你自己去做吧。然后你就需要自己去做,到底放多少培根,放多少面粉,放多少佐料,这都你自己来决定,工序1、2、3,你是怎么实现的,都你自己决定。最后你得到了披萨。

4.4 代码实现

我们用代码来实现上面的不同点:

4.4.1 工厂模式

在这里插入图片描述

class Store
{
void orderPizza(){
Pizza *p = createPizza()
//有了pizza对象后,接下来的工序都是一样的,对用户而言,是透明的;
p->prepare(); //准备材料,放调料
p->bak(); //烘烤
p->cut(); //切块
}
virtual createPizza() = 0; 
}

class AStore : public Store
{
virtual Pizza * createPizza()
{
Pizza * p = new APizza();
return p;
}
}

4.4.2 策略模式
在这里插入图片描述

class Store
{
void orderPizza(){
Pizza *p = MyPizza();
//工序都有,但是具体如何实现,由用户来确定实现;
p->prepare(); 
p->bak(); 
p->cut(); 
}

class MyPizza : public Pizza
{
virtual void prepare(){
//我的pizza我做主,我可以多放点肉,再多来点酱
}
virtual void bak(){
//我想烤的时间长点,这样会更脆
}
virtual  void cut(){
//切块,我想切成20块,像吃西瓜片那样吃披萨
}
}

参考:
https://blog.csdn.net/lh844386434/article/details/16825861
https://www.cnblogs.com/me115/p/3790615.html

C++中的策略模式是一种行为型设计模式,它允许在运行时选择算法的不同实现。该模式定义了一组算法,将它们各自封装起来,并使它们可以互换。 在C++中,可以使用函数指针、函数对象或Lambda表达式来实现策略模式。以下是一个简单的示例: ```c++ #include <iostream> class SortingStrategy { public: virtual void sort(int* arr, int n) = 0; }; class BubbleSort : public SortingStrategy { public: void sort(int* arr, int n) { // Bubble sort implementation std::cout << "Sorting using bubble sort." << std::endl; } }; class QuickSort : public SortingStrategy { public: void sort(int* arr, int n) { // Quick sort implementation std::cout << "Sorting using quick sort." << std::endl; } }; class Sorter { private: SortingStrategy* strategy; public: void setSortingStrategy(SortingStrategy* newStrategy) { strategy = newStrategy; } void sort(int* arr, int n) { strategy->sort(arr, n); } }; int main() { int arr[] = {5, 2, 9, 3, 6}; int n = sizeof(arr) / sizeof(arr[0]); Sorter sorter; BubbleSort bubbleSort; sorter.setSortingStrategy(&bubbleSort); sorter.sort(arr, n); QuickSort quickSort; sorter.setSortingStrategy(&quickSort); sorter.sort(arr, n); return 0; } ``` 在上面的示例中,SortingStrategy是抽象策略类,BubbleSort和QuickSort是具体策略类。Sorter是使用策略模式的上下文类,它可以使用setSortingStrategy方法设置当前使用的SortingStrategy实现,并在sort方法中使用该实现对输入数组进行排序。 运行上面的代码将输出: ``` Sorting using bubble sort. Sorting using quick sort. ``` 这表明程序成功地使用了策略模式来选择不同的排序算法实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值