打字猴:1.700465447e+09
1700465447 //策略模式的运算法则
1700465448
1700465449 public void doSomething();
1700465450
1700465451 }
1700465452
1700465453 具体策略也是非常普通的一个实现类,只要实现接口中的方法就可以,如代码清单18-8所示。
1700465454
1700465455 代码清单18-8 具体策略角色
1700465456
1700465457 public class ConcreteStrategy1 implements Strategy{
1700465458
1700465459 public void doSomething(){
1700465460
1700465461 System.out.println(“具体策略1的运算法则”);
1700465462
1700465463 }
1700465464
1700465465 }
1700465466
1700465467 public class ConcreteStrategy2 implements Strategy{
1700465468
1700465469 public void doSomething(){
1700465470
1700465471 System.out.println(“具体策略2的运算法则”);
1700465472
1700465473 }
1700465474
1700465475 }
1700465476
1700465477 策略模式的重点就是封装角色,它是借用了代理模式的思路,大家可以想想,它和代理模式有什么差别,差别就是策略模式的封装角色和被封装的策略类不用是同一个接口,如果是同一个接口那就成为了代理模式。我们来看封装角色,如代码清单18-9所示。
1700465478
1700465479 代码清单18-9 封装角色
1700465480
1700465481 public class Context{
1700465482
1700465483 //抽象策略
1700465484
1700465485 private Strategy strategy=null;
1700465486
1700465487 //构造函数设置具体策略
1700465488
1700465489 public Context(Strategy_strategy){
1700465490
1700465491 this.strategy=_strategy;
1700465492
1700465493 }
1700465494
1700465495 //封装后的策略方法
1700465496
[ 上一页 ]  [ :1.700465447e+09 ]  [ 下一页 ]