c++设计模式——策略模式

#pragma once
#include "FlyBehavior .h"
class Duck
{
public:
	Duck(FlyBehavior* flybh);
	virtual ~Duck();

	void Swim();

	void PerformFly();

	virtual void Display() = 0;

private:
	FlyBehavior* m_Flybh;
};

#include "Duck.h"
#include <QDeBug>


Duck::Duck(FlyBehavior* flybh)
	:m_Flybh(flybh)
{
}


Duck::~Duck()
{
}

void Duck::Swim()
{
	qDebug() << "Duck Swim" << endl;
}

//void Duck::Disp()
//{
//	qDebug() << "Duck caise" << endl;
//}
void Duck::PerformFly()
{
	m_Flybh->Fly();
}

#pragma once
#include "Duck.h"
class RedHeadDuck :
	public Duck
{
public:
	RedHeadDuck(FlyBehavior* flybh);
	~RedHeadDuck();

	void Display();
};

#include "RedHeadDuck.h"
#include <QDebug>


RedHeadDuck::RedHeadDuck(FlyBehavior* flybh)
	:Duck(flybh)
{
}


RedHeadDuck::~RedHeadDuck()
{
}

void RedHeadDuck::Display()
{
	qDebug() << "I'm Red Head Duck" << endl;
}


#pragma once



class FlyBehavior
{
public:
	virtual void Fly() = 0;
};

#pragma once
#include "FlyBehavior .h"
class FlyWithWings :
	public FlyBehavior
{
public:
	FlyWithWings();
	~FlyWithWings();

	virtual void Fly();
};

#include "FlyWithWings.h"
#include <QDeBug>


FlyWithWings::FlyWithWings()
{
}


FlyWithWings::~FlyWithWings()
{
}

void FlyWithWings::Fly()
{
	qDebug() << "wings fly" << endl;
}


#pragma once
#include "FlyBehavior .h"
class FlyWithRun :
	public FlyBehavior
{
public:
	FlyWithRun();
	~FlyWithRun();

	virtual void Fly();
};

#include "FlyWithRun.h"
#include <QDeBug>


FlyWithRun::FlyWithRun()
{
}


FlyWithRun::~FlyWithRun()
{
}

void FlyWithRun::Fly()
{
	qDebug() << "run fly" << endl;
}



#include "Duck.h"
#include "RedHeadDuck.h"
#include "FlyWithWings.h"
#include "FlyWithRun.h"
//策略模式
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	FlyWithWings* flyWing = new FlyWithWings;
	RedHeadDuck rd(flyWing);
	rd.Swim();
	rd.Display();
	rd.PerformFly();
	
	FlyWithRun* flyRun = new FlyWithRun;
	RedHeadDuck rd1(flyRun);
	rd1.PerformFly();


	return a.exec();
}

    隔离变化,编程到接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
策略模式(Strategy Pattern)是一种行为型设计模式,它允许在运行时动态选择算法的行为。 在C++中,可以通过以下步骤来实现策略模式: 1. 定义一个抽象基类(或接口),该类定义了所有具体策略类所需实现的公共接口。 2. 创建具体的策略类,它们实现了抽象基类中定义的接口,并提供了不同的算法实现。 3. 在需要使用策略的地方,创建一个上下文类,该类持有一个指向抽象基类的指针。上下文类提供了一个接口,用于设置和使用具体的策略对象。 4. 在运行时,根据需求选择合适的具体策略对象,并将其设置给上下文类。 5. 上下文类在需要执行算法时,调用具体策略对象的方法来完成相应的操作。 以下是一个简单的示例代码: ```cpp // 策略接口 class Strategy { public: virtual void execute() = 0; }; // 具体策略类A class ConcreteStrategyA : public Strategy { public: void execute() override { // 实现具体的算法A } }; // 具体策略类B class ConcreteStrategyB : public Strategy { public: void execute() override { // 实现具体的算法B } }; // 上下文类 class Context { private: Strategy* strategy; // 持有一个策略对象的指针 public: void setStrategy(Strategy* strategy) { this->strategy = strategy; } void executeStrategy() { strategy->execute(); } }; // 使用示例 int main() { Context context; ConcreteStrategyA strategyA; context.setStrategy(&strategyA); context.executeStrategy(); ConcreteStrategyB strategyB; context.setStrategy(&strategyB); context.executeStrategy(); return 0; } ``` 在上面的示例中,我们定义了一个策略接口 `Strategy`,并创建了两个具体的策略类 `ConcreteStrategyA` 和 `ConcreteStrategyB`。然后,我们创建了一个上下文类 `Context`,它持有一个指向策略接口的指针,并提供了设置和执行策略的方法。在 `main` 函数中,我们可以根据需要选择具体的策略对象,并通过上下文类来执行相应的算法。 这就是策略模式的基本实现方法。通过使用策略模式,我们可以将算法的实现与使用代码分离,使得代码更加灵活和可维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值