目录
一、鸭子模拟器的实现
二、类图
三、实现鸭子抽象类
四、定义一只绿头鸭
五、定义一只模型鸭
六、定义飞的行为
1.定义飞的行为接口类
2.实现 会飞 的行为
3.实现不会飞的行为
七、实现鸭子 叫 的行为
1.实现叫的接口
2.实现鸭子 呱呱叫 的行为
3.实现鸭子 吱吱叫 的行为
4.实现鸭子 不会叫 的行为
八、测试
十、完整代码实现
十一、参考资料
策略模式体现了 “针对接口编程,而不是针对实现编程”,如有的鸭子会叫,有的鸭子不会叫,把鸭子叫的行为委托给(delegate)给 叫 的接口类去实现,而不是在 鸭子 的类中实现。
注意:
(1)Duck "Has-a" FlyBehavior
(2)Duck "Has-a" QuackBehavior
CDuck.h
#pragma once #include "FlyBehavior.h" #include "QuackBehavior.h" class CDuck { public: CDuck(void); virtual ~CDuck(void); public: void SetFlyBehavior(CFlyBehavior* pFb); void SetQuackBehavior(CQuackBehavior* pQb); public: virtual void PerformQuack(); virtual void PerformFly(); public: void Swim(); virtual void Display() = 0; protected: CFlyBehavior* m_pFlyBehavior; CQuackBehavior* m_pQuackBehavior; };CDuck.cpp
#include "stdafx.h" #include "Duck.h" #include <iostream> #include <afxwin.h> CDuck::CDuck(void) { } CDuck::~CDuck(void) { } void CDuck::SetFlyBehavior(CFlyBehavior* pFb) { m_pFlyBehavior = pFb; } void CDuck::SetQuackBehavior(CQuackBehavior* pQb) { m_pQuackBehavior = pQb; } void CDuck::PerformQuack() { m_pQuackBehavior->quack(); } void CDuck::Swim() { AfxMessageBox(_T("All ducks float, even decoys!"), MB_OK); } void CDuck::Display() { } void CDuck::PerformFly() { m_pFlyBehavior->Fly(); }CMallardDuck.h
#pragma once #include "duck.h" class CMallardDuck : public CDuck { public: CMallardDuck(void); virtual ~CMallardDuck(void); public: void Display(); };CMallardDuck.cpp
#include "stdafx.h" #include "MallardDuck.h" #include "Quack.h" #include <afxwin.h> #include "FlyWithWings.h" CMallardDuck::CMallardDuck(void) { m_pQuackBehavior = new CQuack; //呱呱叫 m_pFlyBehavior = new CFlyWithWings;//会飞 } CMallardDuck::~CMallardDuck(void) { } void CMallardDuck::Display() { AfxMessageBox(_T("我是一直绿头鸭!"), MB_OK); }CModelDuck.h
#pragma once #include "duck.h" class CModelDuck : public CDuck { public: CModelDuck(void); virtual ~CModelDuck(void); public: virtual void Display(); };CModelDuck.cpp
#include "stdafx.h" #include "ModelDuck.h" #include "Quack.h" #include "FlyNoWay.h" #include "MuteQuack.h" CModelDuck::CModelDuck(void) { m_pFlyBehavior = new CFlyNoWay; //不会飞 m_pQuackBehavior = new CMuteQuack; //不会叫 } CModelDuck::~CModelDuck(void) { } void CModelDuck::Display() { AfxMessageBox(_T("我是一只模型鸭!"), MB_OK); }CFlyBehavior.h
#pragma once class CFlyBehavior { public: CFlyBehavior(void); ~CFlyBehavior(void); public: virtual void Fly() = 0; };CFlyBehavior.cpp
#include "stdafx.h" #include "FlyBehavior.h" CFlyBehavior::CFlyBehavior(void) { } CFlyBehavior::~CFlyBehavior(void) { }CFlyBehavior.h
#pragma once #include "flybehavior.h" class CFlyWithWings : public CFlyBehavior { public: CFlyWithWings(void); virtual ~CFlyWithWings(void); public: virtual void Fly(); };CFlyBehavior.cpp
#include "stdafx.h" #include "FlyWithWings.h" #include <afxwin.h> CFlyWithWings::CFlyWithWings(void) { } CFlyWithWings::~CFlyWithWings(void) { } void CFlyWithWings::Fly() { AfxMessageBox(_T("我会飞!"), MB_OK); }CFlyBehavior.h
#pragma once #include "flybehavior.h" class CFlyNoWay : public CFlyBehavior { public: CFlyNoWay(void); virtual ~CFlyNoWay(void); public: virtual void Fly(); };CFlyBehavior.cpp
#include "stdafx.h" #include "FlyNoWay.h" #include <iostream> #include <afxwin.h> CFlyNoWay::CFlyNoWay(void) { } CFlyNoWay::~CFlyNoWay(void) { } void CFlyNoWay::Fly() { AfxMessageBox(_T("我不会飞!"), MB_OK); }CQuackBehavior,h
#pragma once class CQuackBehavior { public: CQuackBehavior(void); ~CQuackBehavior(void); public: virtual void quack() = 0; };CQuackBehavior.cpp
#include "stdafx.h" #include "QuackBehavior.h" #include <iostream> #include <afxwin.h> CQuackBehavior::CQuackBehavior(void) { } CQuackBehavior::~CQuackBehavior(void) { }CQuack.h
#pragma once #include "quackbehavior.h" class CQuack : public CQuackBehavior { public: CQuack(void); virtual ~CQuack(void); public: virtual void Quack(); };CQuack.cpp
#include "stdafx.h" #include "Quack.h" CQuack::CQuack(void) { } CQuack::~CQuack(void) { } void CQuack::Quack() { AfxMessageBox(_T("呱呱叫!"), MB_OK); }CSqueak.h
#pragma once #include "quackbehavior.h" class CSqueak : public CQuackBehavior { public: CSqueak(void); virtual ~CSqueak(void); public: virtual void Quack(); };CSqueak.cpp
#include "stdafx.h" #include "Squeak.h" #include <iostream> #include <afxwin.h> CSqueak::CSqueak(void) { } CSqueak::~CSqueak(void) { } void CSqueak::Quack() { AfxMessageBox(_T("吱吱叫!"), MB_OK); }CMuteQuack.h
#pragma once #include "quackbehavior.h" class CMuteQuack : public CQuackBehavior { public: CMuteQuack(void); virtual ~CMuteQuack(void); public: virtual void Quack(); };CMuteQuack.cpp
#include "stdafx.h" #include "MuteQuack.h" #include <iostream> #include <afxwin.h> CMuteQuack::CMuteQuack(void) { } CMuteQuack::~CMuteQuack(void) { } void CMuteQuack::Quack() { AfxMessageBox(_T("不会叫!"), MB_OK); }CMiniDuckSimulator.h
#include "stdafx.h" #include "MiniDuckSimulator.h" #include "Duck.h" #include "MallardDuck.h" #include "ModelDuck.h" #include "FlyRocketPowered.h" CMiniDuckSimulator::CMiniDuckSimulator(void) { } CMiniDuckSimulator::~CMiniDuckSimulator(void) { } void CMiniDuckSimulator::Test() { CDuck* mallard = new CMallardDuck; //绿头鸭 mallard->PerformQuack(); mallard->PerformFly(); // CDuck *pModel = new CModelDuck; //模型鸭 pModel->PerformFly(); pModel->SetFlyBehavior(new CFlyRocketPowered); pModel->PerformFly(); }CMiniDuckSimulator.cpp
#pragma once class CMiniDuckSimulator { public: CMiniDuckSimulator(void); ~CMiniDuckSimulator(void); public: void Test(); };https://github.com/mc-liyanliang/Design-patterns/tree/master/Strategy%20Pattem
《Head First设计模式》