一、策略模式:定义了算法族,并分别封装,使算法的变化独立于客户。
二、示例:
        1、设计UML图:       
        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{

    public void 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{

    public void laugh(){
         System.out.println("呵呵呵!");
    }

}

  //“笑”行为算法族实现类:“哈哈哈”
public class HaHaHa implements Laugh{

    public void 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("吃");
    }

    //代理模式:“说”行为算法族代理此方法
    public void speak(){
         speak.speak();
    }

    //代理模式:“笑”行为算法族代理此方法
    public void laugh(){
         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:英国人喝百事可乐
    public void drink(){
         System.out.println("喝百事可乐");
    }

}

//算法使用者:美国人
public class AmericanP extends People{

    public AmericanP(){
         factory.getLaughInstance("HeiHeiHei");
         factory.getSpeakInstance("English");
    }

    //drink:美国人喝健力宝
    public void 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.drink();
         people.eat();
         people.speak(); 
         people.laugh();
         people.setLaugh(factory.getLaughInstance("HaHaHa"));
         people.drink();
         people.eat();
         people.speak();
         people.laugh();

         //用简单工厂制造美国人
         people = factory.getPeopleInstance("AmericanP");
         people.drink();
         people.eat();
         people.speak();
         people.laugh();
         people.setSpeak(factory.getSpeakInstance("Chinese"));
         people.setLaugh(factory.getLaughInstance("HeHeHe"));
         people.drink();
         people.eat(); 
         people.speak();
         people.laugh();

     }
}