[C++]策略模式

在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。

在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

C++代码如下:

// simple_factory.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// 参考大话设计模式 - 策略模式

#include <iostream>

using namespace std;

// 不同策略的接口
class CashSuper {
public:
	virtual double acceptCash(double money) = 0;
};

// 具体的策略实现子类
// 正常收费
class CashNormal : public CashSuper {
public:
	double acceptCash(double money) {
		return money;
	}
};

// 具体的策略实现子类
// 打折收费
class CashRebate : public CashSuper {
public:
	CashRebate(double moneyRebate) {
		moneyRebate_ = moneyRebate;
	}

	double acceptCash(double money) {
		return money * moneyRebate_;
	}

private:
	double moneyRebate_ = 0;
};

// 具体的策略实现子类
// 返利收费
class CashReturn : public CashSuper {
public:
	CashReturn(double total, double ret) {
		total_ = total;
		return_ = ret;
	}

	double acceptCash(double money) {
		if (money > total_) {
			return money - return_;
		}
		else {
			return money;
		}
	}

private:
	double total_ = 0;
	double return_ = 0;
};

// 根据具体不同的策略,创建不同的对象,包含策略的具体算法
class CashContext {
public:
	CashContext(int ntype) {
		switch (ntype) {
		case 1:
		{
			cashSuper_ = new CashNormal();
			break;
		}
		case 2:
		{
			cashSuper_ = new CashRebate(0.8);
			break;
		}
		case 3:
		{
			cashSuper_ = new CashReturn(500, 300);
			break;
		}
		default:
			break;
		}
	}

	~CashContext() {
		if (cashSuper_ != nullptr) {
			delete cashSuper_;
			cashSuper_ = nullptr;
		}
	}

	double GetCashResult(double money) {
		return cashSuper_->acceptCash(money);
	}

private:
	CashSuper* cashSuper_ = nullptr;
};

int main()
{
	// 客户端只关心调用策略类型,而不关心策略的具体实现
	int ntype = 3;
	CashContext* cashContext = new CashContext(ntype);
	cout << cashContext->GetCashResult(1000) << "\n";

	delete cashContext;
	cashContext = nullptr;

	ntype = 2;
	cashContext = new CashContext(ntype);
	cout << cashContext->GetCashResult(1000) << "\n";

	delete cashContext;
	cashContext = nullptr;

	return 0;
}


github源码路径:https://github.com/dangwei-90/Design-Mode

持续更新中

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值