Headfirst设计模式的C++实现——策略模式(Strategy)
生活随笔
收集整理的這篇文章主要介紹了
Headfirst设计模式的C++实现——策略模式(Strategy)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言
最近在學(xué)習(xí)《Headfirst設(shè)計(jì)模式》,里面的例子都是Java的。但是我對(duì)Java并不熟悉,所以試著用C++來(lái)實(shí)現(xiàn)書(shū)中的例子。
?
先來(lái)看看Duck以及子類(lèi)
Duck.h
1 #include <iostream> 2 #include "FlyBehavior.h" 3 4 class Duck 5 { 6 private: 7 FlyBehavior *m_p_fly_behavior; 8 public: 9 virtual void quack() { std::cout << "--quack--" << std::endl; } 10 virtual void swim() { std::cout << "--swim--" << std::endl; } 11 virtual void display() { std::cout << "--duck--" << std::endl; } 12 13 virtual void fly() { if ( m_p_fly_behavior) { m_p_fly_behavior->fly(); } }; 14 void set_fly_behavior( FlyBehavior *p_fly_behavior) { m_p_fly_behavior = p_fly_behavior; } 15 16 Duck() : m_p_fly_behavior(NULL) {} 17 virtual ~Duck() { if (m_p_fly_behavior) { delete m_p_fly_behavior; } } 18 };?
MallardDuck.h
1 #include "Duck.h" 2 #include "FlyWithWings.h" 3 4 class MallardDuck : public Duck 5 { 6 public: 7 void display() { std::cout << "--MallardDuck--" << std::endl; } 8 MallardDuck() { set_fly_behavior( new FlyWithWings() ); } 9 };?
RedheadDuck.h
1 #include "FlyWithWings.h" 2 class RedheadDuck : public Duck 3 { 4 public: 5 void display() { std::cout << "--RedheadDuck--" << std::endl; } 6 RedheadDuck() { set_fly_behavior( new FlyWithWings() ); } 7 };?
RubberDuck.h
1 #include "FlyNoWay.h" 2 3 class RubberDuck : public Duck 4 { 5 public: 6 void display() { std::cout << "--RubberDuck--" << std::endl; } 7 void quack() { std::cout << "--squeak--" << std::endl; } 8 RubberDuck() { set_fly_behavior( new FlyNoWay() ); } 9 };?
再來(lái)看看FlyBehavior類(lèi)及其子類(lèi)
FlyBehavior.h
1 #include <iostream> 2 3 class FlyBehavior 4 { 5 public: 6 virtual void fly() {}; 7 virtual ~FlyBehavior() {}; 8 };?
FlyWithWings.h
1 #include "FlyBehavior.h" 2 3 class FlyWithWings : public FlyBehavior 4 { 5 public: 6 void fly () { std::cout << "--fly with wings--" << std::endl; } 7 };?
FlyNoWay.h
1 #include "FlyBehavior.h" 2 3 class FlyNoWay : public FlyBehavior 4 { 5 public: 6 void fly() { std::cout << "--can not fly--" << std::endl; } 7 };?
最后是主函數(shù)
main.cpp
1 #include "MallardDuck.h" 2 #include "RedheadDuck.h" 3 #include "RubberDuck.h" 4 #include <vector> 5 6 void test() 7 { 8 std::vector<Duck *> p_ducks; 9 p_ducks.push_back( new MallardDuck() ); 10 p_ducks.push_back( new RedheadDuck() ); 11 p_ducks.push_back( new RubberDuck() ); 12 13 for (std::vector<Duck *>::iterator it = p_ducks.begin(); it < p_ducks.end(); it ++) 14 { 15 std::cout << std::endl; 16 (*it)->display(); 17 (*it)->quack(); 18 (*it)->swim(); 19 (*it)->fly(); 20 delete (*it); 21 } 22 } 23 24 int main() 25 { 26 while (true) 27 { 28 test(); 29 } 30 }?
稍作解釋
main函數(shù)中死循環(huán)的調(diào)用test函數(shù)的意圖是測(cè)試以上實(shí)現(xiàn)有沒(méi)有內(nèi)存泄露。
轉(zhuǎn)載于:https://www.cnblogs.com/ren-yu/p/4928809.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Headfirst设计模式的C++实现——策略模式(Strategy)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode Find the D
- 下一篇: Java使用UDP聊天程序