天天看點

Strategy Pattern

一、政策模式:定義了算法族,并分别封裝,使算法的變化獨立于客戶。

二、示例:

        1、設計uml圖:       

Strategy Pattern

        2、coding:

              1>.“說”行為算法族  

//“說”行為算法族:“說”行為接口

public interface speak{

    public void speak();

}

//“說”行為算法族實作類:“漢語”

public class chinese implements speak{

    public void speak(){

         system.out.println("漢語!");

    }

//“說”行為算法族實作類:“英語”

public class english implements speak{

         system.out.println("英語!");

              2>.“笑”行為算法族

//“笑”行為算法族:“笑”行為接口

public interface laugh{

    public void laugh();

//“笑”行為算法族實作類:“嘿嘿嘿”

public class heiheihei implements laugh{

    public void laugh(){

         system.out.println("嘿嘿嘿!");

//“笑”行為算法族實作類:“呵呵呵”

public class hehehe implements laugh{

         system.out.println("呵呵呵!");

  //“笑”行為算法族實作類:“哈哈哈”

public class hahaha implements laugh{

         system.out.println("哈哈哈!");

              3>.算法使用者

//算法使用者:人

public abstract class people{

    //行為特定接口變量

    speak speak;

    laugh laugh;

    simplefactory factory = new simplefactory();

    //constructor:設定預設“說”、“笑”行為對象

    public people(){

         speak = factory.getspeakinstance("chinese");

         laugh = factory.getlaughinstance("hahaha");

    //設定“說”行為對象

    public void setspeak(speak speak){

         this.speak = speak;

    //設定“笑”行為對象

    public void setlaugh(laugh laugh){

         this.laugh = laugh;

    //喝:特定行為由各個子類自己實作

    public abstract void drink();

    //吃

    public void eat(){

         system.out.println("吃");

    //代理模式:“說”行為算法族代理此方法

         speak.speak();

    //代理模式:“笑”行為算法族代理此方法

         laugh.laugh();

//算法使用者:中國人

public class chinesep extends people{

    //運用父類預設行為對象,是以注釋

    /*public chinesep(){

         simplefactory.getspeakinstance("hahaha");

         simplefactory.getspeakinstance("chinese");

    }*/

    //drink:中國人喝礦泉水

    public void drink(){

         system.out.println("喝礦泉水");

//算法使用者:英國人

public class englishp extends people{

    public englishp(){

         factory.getlaughinstance("hehehe");

         factory.getspeakinstance("english");

    //drink:英國人喝百事可樂

         system.out.println("喝百事可樂");

//算法使用者:美國人

public class americanp extends people{

    public americanp(){

         factory.getlaughinstance("heiheihei");

    //drink:美國人喝健力寶

         system.out.println("喝健力寶");

              4>.利用簡單工廠模式建立people、speak、laugh對象

//簡單工廠類

public class simplefactory{

    //people簡單工廠方法

    public people getpeopleinstance(string type){

         people people = null;

         if(type.equals("americanp")){

             people = new americanp();

         }else if(type.equals("chinesep")){

             people = new chinesep();

         }else if(type.equals("englishp")){

             people = new englishp();

         }

         return people;

    //speak簡單工廠方法

    public speak getspeakinstance(string type){

         speak speak = null;

         if(type.equals("chinese")){

              speak = new chinese();

         }else if(type.equals("english")){

              speak = new english(); 

         }

         return speak;

    //laugh簡單工廠方法

    public laugh getlaughinstance(string type){

         laugh laugh = null;

         if(type.equals("hahaha")){

              laugh = new hahaha();

         }else if(type.equals("hehehe")){

              laugh = new hehehe();

         }else if(type.equals("heiheihei")){

              laugh = new heiheihei(); 

         return laugh;

              5>.測試類

//測試類:測試政策模式

public class test{

     public static void main(string[] args){

         //執行個體化簡單工廠類

         simplefactory factory = new simplefactory();  

         //用簡單工廠制造中國人

         people people = factory.getpeopleinstance("chinesep");

         people.drink(); 

         people.eat(); 

         people.laugh(); 

         people.speak(); 

         people.setspeak(factory.getspeakinstance("english"));

         people.drink();

         people.eat();

         people.speak(); 

         people.laugh();

         //用簡單工廠制造英國人

         people = factory.getpeopleinstance("englishp");

         people.setlaugh(factory.getlaughinstance("hahaha"));

         people.speak();

         people.laugh();

         //用簡單工廠制造美國人

         people = factory.getpeopleinstance("americanp");

         people.setspeak(factory.getspeakinstance("chinese"));

         people.setlaugh(factory.getlaughinstance("hehehe"));

         people.eat(); 

         people.speak();

     }

繼續閱讀