标签:os 使用 ar java sp 2014 on new bs
/**
* 抽象的的策略角色
*
* @date 2014-10-4上午10:04:33
*/
public interface Stratrgy {
public void dosomething();
}
public class CreateStrategy1 implements Stratrgy {
@Override
public void dosomething() {
System.out.println("执行具体的算法1。。。。。。。。。。。。");
}
}
public class CreateStrategy2 implements Stratrgy {
@Override
public void dosomething() {
System.out.println("执行具体的算法2。。。。。。。。。。。。");
}
}
public class Context {
private Stratrgy sty= null;
public Context(Stratrgy _sty) {
this.sty = _sty;
}
public void doAnything(){
this.sty.dosomething();
}
}
/**
*
* 策略模式的优点:
* 1.算法可以自由的切换
* 2.避免使用多重条件判断
* 3.扩展性良好
* 策略模式的缺点:
* 1.策略类数量增多
* 2.所有的策略类都需要对外暴露(可以用工厂方法模式、代理模式或享元模式来修正缺陷)
*/
public static void main(String[] args) {
Stratrgy sty1 = new CreateStrategy1();
Stratrgy sty2 = new CreateStrategy2();
Context context = new Context(sty1);
context.doAnything();
}
标签:os 使用 ar java sp 2014 on new bs
原文地址:http://my.oschina.net/u/586094/blog/326886