《Head First 设计模式》C++实现【策略模式(Strategy Pattern)】
摘要
《Head First 设计模式》书中第2章——策略模式(Strategy Pattern)的C++代码实现。
策略模式(Strategy Pattern):定义了算法簇,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。
实现代码
//《Head First 设计模式》C++实现【策略模式(Strategy Pattern)】
// 定义了算法簇,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。
#include <iostream>
using namespace std;
//接口
class CFlyBehavior
{
public:
virtual void Fly() = 0;
};
class CQuackBehavior
{
public:
virtual void Quack() = 0;
};
class CDuck
{
public:
CFlyBehavior *p_fly_behavior;
CQuackBehavior *p_quack_behavior;
virtual void Display() = 0;//every ducks is different
void Swim()
{
printf("All duck able to swiming.\n");
}
void PerformFly()
{
p_fly_behavior->Fly();
}
void PerformQuack()
{
p_quack_behavior->Quack();
}
void SetFlyBehavior(CFlyBehavior *p_fly_behavior)
{
this->p_fly_behavior = p_fly_behavior;
}
void SetQuackBehavior(CQuackBehavior *p_quack_behavior)
{
this->p_quack_behavior = p_quack_behavior;
}
// 鸭子的其他方法……
};
//行为对象
class CFlyWithWings : public CFlyBehavior
{
public:
void Fly() override
{
printf("I'm flying!!\n");
}
};
class CFlyNoWay : public CFlyBehavior
{
public:
void Fly() override
{
printf("I can't flying.\n");
}
};
class CQuack : public CQuackBehavior
{
public:
void Quack() override
{
printf("Quack quack quack ...\n");
}
};
class CSQuack : public CQuackBehavior
{
public:
void Quack() override
{
printf("Squack squack squack ...\n");
}
};
class CMuteQuack : public CQuackBehavior
{
public:
void Quack() override
{
}
};
//鸭子对象
//绿头鸭
class CMallarDuck : public CDuck
{
public:
CMallarDuck()
{
this->p_fly_behavior = new CFlyNoWay();
this->p_quack_behavior = new CQuack();
}
void Display() override
{
printf("I am MallarDuck.\n");
}
};
//橡皮鸭
class CRubberDuck : public CDuck
{
public:
CRubberDuck()
{
this->p_fly_behavior = new CFlyNoWay();
this->p_quack_behavior = new CSQuack();
}
void Display() override
{
printf("I am RubberrDuck.\n");
}
};
int main()
{
CDuck *p_mallar_duck = new CMallarDuck();
CDuck *p_rubber_duck = new CRubberDuck();
p_mallar_duck->Display();
p_mallar_duck->Swim();
p_mallar_duck->PerformFly();
p_mallar_duck->PerformQuack();
p_rubber_duck->Display();
p_rubber_duck->Swim();
p_rubber_duck->PerformFly();
p_rubber_duck->PerformQuack();
p_rubber_duck->SetFlyBehavior(new CFlyWithWings);
p_rubber_duck->PerformFly();
system("pause");
return 0;
}
原文作者:Liber-coder
转载自:https://blog.csdn.net/a435262767/article/details/91047356