标签:interface 策略 dos 影子 article pat 关注 span 而在
1 package com.dp.strategypattern; 2 3 public interface IStrategy { 4 public void doSomething(); 5 }
具体策略类
1 package com.dp.strategypattern; 2 3 public class Strategy1 implements IStrategy { 4 @Override 5 public void doSomething() { 6 System.out.println("具体策略------》 1"); 7 } 8 }
1 package com.dp.strategypattern; 2 3 public class Strategy2 implements IStrategy { 4 @Override 5 public void doSomething() { 6 System.out.println("具体策略------》 2"); 7 } 8 }
封装类
1 package com.dp.strategypattern; 2 3 public class Context { 4 private IStrategy strategy; 5 6 public Context(IStrategy strategy){ 7 this.strategy = strategy; 8 } 9 10 public void execute(){ 11 strategy.doSomething(); 12 } 13 }
测试类
1 package com.dp.strategypattern; 2 3 public class StrategyTest { 4 5 public static void main(String[] args){ 6 Context context; 7 System.out.println("execute Strategy 1"); 8 context = new Context(new Strategy1()); 9 context.execute(); 10 11 System.out.println("execute Strategy 2"); 12 context = new Context(new Strategy2()); 13 context.execute(); 14 } 15 }
----------- output ----------------
execute Strategy 1
具体策略------》 1
execute Strategy 2
具体策略------》 2
http://blog.csdn.net/monkey_d_meng/article/details/6005764
标签:interface 策略 dos 影子 article pat 关注 span 而在
原文地址:http://www.cnblogs.com/zi-yao/p/6195813.html