标签:缺点 ack cas system sys 数据 代码 实现类 方案
public interface Strategy { public void operate(); }
public class BackDoor implements IStrategy { @Override public void operate() { System.out.println("找乔国老帮忙,让吴国太给孙权施加压力,使孙权不能杀刘备"); } }
1 public class GivenGreenLight implements IStrategy { 2 @Override 3 public void operate() { 4 System.out.println("求吴国太开个绿灯,放行"); 5 } 6 }
1 public class BlackEnemy implements IStrategy { 2 @Override 3 public void operate() { 4 System.out.println("孙夫人断后,挡住追兵"); 5 } 6 }
1 public class Context { 2 private Strategy strategy; 3 //构造函数,要你使用哪个妙计 4 public Context(Strategy strategy){ 5 this.strategy = strategy; 6 } 7 public void setStrategy(Strategy strategy){ 8 this.strategy = strategy; 9 } 10 public void operate(){ 11 this.strategy.operate(); 12 } 13 }
1 public class LiuBei { 2 3 public static void main(String[] args) { 4 Context context; 5 6 System.out.println("----------刚到吴国使用第一个锦囊---------------"); 7 context = new Context(new BackDoor()); 8 context.operate(); 9 System.out.println("\n"); 10 11 System.out.println("----------刘备乐不思蜀使用第二个锦囊---------------"); 12 context.setStrategy(new GivenGreenLight()); 13 context.operate(); 14 System.out.println("\n"); 15 16 System.out.println("----------孙权的追兵来了,使用第三个锦囊---------------"); 17 context.setStrategy(new BlackEnemy()); 18 context.operate(); 19 System.out.println("\n"); 20 } 21 }
标签:缺点 ack cas system sys 数据 代码 实现类 方案
原文地址:http://www.cnblogs.com/the-wang/p/7323232.html