HeadFirst设计模式-策略模式
策略模式:定义了算法簇,分别封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。
java实现:
package a_StrategyPattern._01_duck; public abstract class Duck { protected FlyBehavior flyBehavior; protected QuackBehavior quackBehavior; public void setFlyBehavior(FlyBehavior flyBehavior) { this.flyBehavior = flyBehavior; } public void setQuackBehavior(QuackBehavior quackBehavior) { this.quackBehavior = quackBehavior; } public void swim() { System.out.println("鸭子游泳。。。"); } public abstract void display(); public void performFly() { flyBehavior.fly(); } public void performQuack() { quackBehavior.quack(); } } //-------------------------------------------- package a_StrategyPattern._01_duck; public interface FlyBehavior { public void fly(); } //-------------------------------------------- package a_StrategyPattern._01_duck; public interface QuackBehavior { public void quack(); } //-------------------------------------------- package a_StrategyPattern._01_duck; /** * 用翅膀飞 * @author ljq * */ public class FlyWithWings implements FlyBehavior { @Override public void fly() { System.out.println("飞来飞去...."); } } //-------------------------------------------- package a_StrategyPattern._01_duck; public class FlyNoWay implements FlyBehavior { @Override public void fly() { System.out.println("不会飞。"); } } //-------------------------------------------- package a_StrategyPattern._01_duck; /** * 正常的呱呱叫 * @author ljq * */ public class Quack implements QuackBehavior { @Override public void quack() { System.out.println("呱呱叫..."); } } //-------------------------------------------- package a_StrategyPattern._01_duck; /** * 吱吱叫的叫法 * @author ljq * */ public class Squeak implements QuackBehavior { @Override public void quack() { System.out.println("吱吱叫..."); } } //-------------------------------------------- package a_StrategyPattern._01_duck; /** * 绿头鸭 */ public class MallardDuck extends Duck { public MallardDuck() { this.flyBehavior = new FlyWithWings(); this.quackBehavior = new Quack(); } @Override public void display() { System.out.println("this is a real duck."); } } //---------------------------------------------- package a_StrategyPattern._01_duck; /** * 橡皮鸭子 * @author ljq * */ public class RubberDuck extends Duck { public RubberDuck() { this.flyBehavior = new FlyNoWay(); this.quackBehavior = new Squeak(); } @Override public void display() { System.out.println("this is a rubber duck."); } } //---------------------------------------------
C++代码
#pragma once #include "WeaponBehavior.h" class Character { public: Character() { weapon = 0; } virtual ~Character() { } public: virtual int fight() = 0 //纯虚函数可以有默认函数体 { this->weapon->useWeapon(); return 0; } protected: WeaponBehavior* weapon; }; //------------------------------------- #pragma once #include "Character.h" class King : public Character { public: King(WeaponBehavior* weapon) { this->weapon = weapon; } virtual ~King() { } void setWeaponBehavior(WeaponBehavior* weapon) { this->weapon = weapon; } virtual int fight() { //this->weapon->useWeapon(); //return 0; return Character::fight(); } }; //------------------------------------- #pragma once #include "Character.h" #include "AxeBehavior.h" class Queen : public Character { public: Queen(WeaponBehavior* weapon) { this->weapon = weapon; } virtual ~Queen() { } void setWeaponBehavior(WeaponBehavior* weapon) { this->weapon = weapon; } virtual int fight() { this->weapon->useWeapon(); return 0; } }; //------------------------------------- #pragma once //interface class WeaponBehavior { public: WeaponBehavior() { } virtual ~WeaponBehavior() { } public: virtual int useWeapon() = 0; }; //------------------------------------- #pragma once #include "WeaponBehavior.h" #include <iostream> using namespace std; class AxeBehavior : public WeaponBehavior { public: AxeBehavior() { } virtual ~AxeBehavior() { } virtual int useWeapon() { cout << "用斧子砍\n"; return 0; } }; //------------------------------------- #pragma once #include "WeaponBehavior.h" #include <iostream> using namespace std; class BowAndArrowBehavior : public WeaponBehavior { public: BowAndArrowBehavior() { } virtual ~BowAndArrowBehavior() { } virtual int useWeapon() { cout << "用弓箭射击\n"; return 0; } }; //------------------------------------- #pragma once #include "WeaponBehavior.h" #include <iostream> using namespace std; class KnifeBehavior : public WeaponBehavior { public: KnifeBehavior() { } virtual ~KnifeBehavior() { } virtual int useWeapon() { cout << "用匕首刺杀\n"; return 0; } }; //------------------------------------- #include <iostream> #include "Queen.h" #include "King.h" #include "AxeBehavior.h" #include "BowAndArrowBehavior.h" using namespace std; int main(){ cout << "hello...\n"; AxeBehavior axe; Queen qu(&axe); qu.fight(); BowAndArrowBehavior baa; qu.setWeaponBehavior(&baa); qu.fight(); King k(&baa); k.fight(); k.setWeaponBehavior(&axe); k.fight(); system("PAUSE"); return 0; }
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。