design_pattern_strategy
2015-03-04 11:08
489 查看
This post implement strategy pattern, described in <Head First Design Pattern>.
Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. The class structure is as follows
The main function is as follows
Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. The class structure is as follows
class FlyBehavior{ public: virtual void fly() = 0; }; class FlyWithWings : public FlyBehavior{ public: virtual void fly(){ std::cout << "I am flying!" << std::endl; } }; class FlyNoWay : public FlyBehavior{ public: virtual void fly(){ std::cout << "I cannot fly!" << std::endl; } }; class QuackBehavior{ public: virtual void quack() = 0; }; class Quack : public QuackBehavior{ public: virtual void quack(){ std::cout << "Quack!" << std::endl; } }; class Suqeak : public QuackBehavior{ public: virtual void quack(){ std::cout << "Quqeak!" << std::endl; } }; class MuteQuack : public QuackBehavior{ public: virtual void quack(){ std::cout << "MuteQuack!" << std::endl; } }; class Duck{ protected: FlyBehavior* fb; QuackBehavior* qb; public: Duck(){ fb = NULL; qb = NULL; } Duck(FlyBehavior* fb_, QuackBehavior* qb_){ fb = fb_; qb = qb_; }; Duck(const Duck& duck){ this->fb = duck.fb; this->qb = duck.qb; }; void operator=(const Duck& duck){ this->fb = duck.fb; this->qb = duck.qb; }; virtual void performQuack() const = 0; virtual void performFly() const = 0; virtual void display() const = 0; void swim(){ std::cout << "Swimming!" << std::endl; }; void setFlyBehavior(FlyBehavior* fb_){ fb = fb_; } void setQuackBehavior(QuackBehavior* qb_){ qb = qb_; } virtual ~Duck(){ delete qb; delete fb; fb = NULL; qb = NULL; } }; class ModelDuck : public Duck{ public: ModelDuck() : Duck(){} ModelDuck(FlyBehavior* fb_, QuackBehavior* qb_){ fb = fb_; qb = qb_; } void performQuack() const { qb->quack(); } void performFly() const { fb->fly(); } void display() const { std::cout << "I am one model duck:)" << std::endl; } };
The main function is as follows
#include <iostream> #include <memory> #include <./duck.hpp> int main(){ FlyNoWay* fb = new FlyNoWay(); MuteQuack* qb = new MuteQuack(); ModelDuck ducky = ModelDuck(fb, qb); ducky.performFly(); ducky.performQuack(); delete fb; fb = NULL; delete qb; qb = NULL; return 0; }
相关文章推荐
- Using the Strategy pattern in algorithm design
- head first design pattern -Strategy
- 策略设计模式 Strategy Design Pattern
- Design Pattern Explained 读书笔记四——Strategy
- design_pattern_strategy
- 《Head First Design Patterns》笔记一:策略模式(Strategy Pattern)
- 8.2.1 策略设计模式(The strategy design pattern)
- Strategy Design Pattern 策略设计模式
- Strategy Design Pattern(策略模式)
- Strategy pattern in algorithm design
- 设计模式1--策略模式(the strategy design pattern)
- Strategy Design Pattern
- 策略模式 in Java (Strategy Design Pattern)
- 设计模式学习—策略模式(Strategy Design Pattern)
- the difference between state and strategy design pattern
- strategy design pattern and composite design pattern
- design pattern notes [7] - state,strategy,memento,mediator,and iterator
- design pattern notes [7] - state,strategy,memento,mediator,and iterator
- Strategy design pattern
- Learning OpenCV Lecture 2 (Using the Strategy pattern in algorithm design)