設計模式學習筆記1:策略模式

目標:鴨子有不一樣種類,會不一樣的叫法和飛法,都會游泳,能夠顯示。能動態改變叫法和飛法 算法

public interface FlyBehaviour {
    void fly();
} ide

-------------------------------------- this

public interface QuackBehaviour {
    void quack();
} orm

---------------------------------------- 繼承

public class Duck {
    FlyBehaviour flyBehaviour;
    QuackBehaviour quackBehaviour;
    void performFly(){
        flyBehaviour.fly();
    }
    void performQuack(){
        quackBehaviour.quack();
    }
    void display(){
        System.out.println("I'm a duck");
    }
}
------------------------------------- 接口

public class FlyWithWing implements FlyBehaviour {
    @Override
    public void fly(){
        System.out.println("fly with wing");
    }
}
------------------------------------------ it

public class Quack implements QuackBehaviour{
    @Override
    public void quack() {
        System.out.println("Quack, quack, quack !");
    } io

} form

------------------------------------------------ class

public class MallarDuck extends Duck {
    public MallarDuck(){
        this.flyBehaviour=new FlyWithWing();
        this.quackBehaviour=new Quack();
    }
    void display(){
        System.out.println("I'm a mallar duck");
    }
}
----------------------------------------------

public class Farm {
    public void Farm(){
       
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        Duck duck=new MallarDuck();
        duck.display();
        duck.performFly();
        duck.performQuack();

    }

}

===========================

result:

I'm a mallar duck
fly with wing
Quack, quack, quack !

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

讓不一樣的鴨子會叫會飛,把具體行爲的實現抽離出超類,行爲做爲接口變量存在於超類中,超類不需理會其行爲的不一樣。

多用組合,少用繼承

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

改進:動態改變鴨子行爲

public class Duck {
    FlyBehaviour flyBehaviour;
    QuackBehaviour quackBehaviour;
    void performFly(){
        flyBehaviour.fly();
    }
    void performQuack(){
        quackBehaviour.quack();
    }
    void display(){
        System.out.println("I'm a duck");
    }
    void swim(){
        System.out.println("I can swim ,'cause I'm a duck");
    }
    //can change behavior!
    public void setFlyBehaviour(FlyBehaviour flyBehaviour) {
        this.flyBehaviour = flyBehaviour;
    }
    public void setQuackBehaviour(QuackBehaviour quackBehaviour) {
        this.quackBehaviour = quackBehaviour;
    }
   
}

-------------------------------------------------------------------
public class DuckModule extends Duck {
    public DuckModule(){
        flyBehaviour=new FlyNoWay();
        quackBehaviour=new Quack();
    }
    public void display(){
        System.out.println("I'm a module duck");
    }
}
----------------------------------------------------------

public class FlyNoWay implements FlyBehaviour{
    @Override
    public void fly() {
        //Unable to fly
        System.out.println("I can't fly");
    }

}

-----------------------------------------------------------
public class FlyRocketPowered implements FlyBehaviour{
    @Override
    public void fly() {
        System.out.println("I'm flying with a rocket!");
    }

}

-------------------------------------------------------
public class Farm {
    public void Farm(){
       
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        Duck moduleDuck=new DuckModule();
        moduleDuck.display();
        moduleDuck.performFly();
        moduleDuck.performQuack();
        moduleDuck.setFlyBehaviour(new FlyRocketPowered());
        moduleDuck.performFly();       
    }

}

=============================

result:

I'm a module duck
I can't fly
Quack, quack, quack !
I'm flying with a rocket!

==============================

策略模式(Strategy Pattern)定義了算法族,分別封裝起來,讓他們之間能夠互相替換,此模式讓算法的變化獨立於使用算法的客戶

相關文章
相關標籤/搜索