策略设计模式

来源:互联网 发布:淘宝客招募令模板 编辑:程序博客网 时间:2024/05/17 09:18

       策略设计模式是定义了一系列的替代方案,供你选择。如A,B,C,需要什么就传什么。

     如在商场中,有很多促销的活动,每个促销的活动就是一个策略,何为策略,就是一个方法或者思路呗。有很多的策略,每个策略解决一个问题,你在什么情况下,根据需要调用哪一个策略。

     看如下的例子吧:

     public interface IMethod {

public void show();

    }

    public class A_MehtodImp implements IMethod{

@Override

public void show() {

       System.out.println("A method...");

}

   }

   public class B_MehtodImp implements IMethod{

@Override

public void show() {

       System.out.println("B method...");

}

  }

 public class C_MethodImp implements IMethod{

@Override

public void show() {

        System.out.println("C mehtod...");

}

}

 public class Context {

private IMethod method;

public void setMethod(IMethod method){

this.method=method;

}

public void show(){

method.show();

}

}

 public class Client {

public static void main(String[] args) {

         Context context=new Context();

    //     context.setMethod(new B_MehtodImp());

         context.setMethod(new C_MethodImp());

         context.show();

}

}

原创粉丝点击