1 三个妙计是同一类型的东东,那咱就写个接口: 2 package com.cbf4life.strategy; 3 /** 4 * @author cbf4Life [email protected] 5 * I'm glad to share my knowledge with you all. 6 * 首先定一个策略接口,这是诸葛亮老人家给赵云的三个锦囊妙计的接口 7 * 8 */ 9 public interface IStrategy { 10 11 //每个锦囊妙计都是一个可执行的算法 12 public void operate(); 13 } 14 第 4 页 15 您的设计模式 16 然后再写三个实现类,有三个妙计嘛: 17 18 package com.cbf4life.strategy; 19 /** 20 * @author cbf4Life [email protected] 21 * I'm glad to share my knowledge with you all. 22 * 找乔国老帮忙,使孙权不能杀刘备 23 */ 24 public class BackDoor implements IStrategy { 25 public void operate() { 26 System.out.println("找乔国老帮忙,让吴国太给孙权施加压力"); 27 } 28 } 29 package com.cbf4life.strategy; 30 /** 31 * @author cbf4Life [email protected] 32 * I'm glad to share my knowledge with you all. 33 * 求吴国太开个绿灯 34 */ 35 public class GivenGreenLight implements IStrategy { 36 public void operate() { 37 System.out.println("求吴国太开个绿灯,放行!"); 38 } 39 } 40 package com.cbf4life.strategy; 41 /** 42 * @author cbf4Life [email protected] 43 * I'm glad to share my knowledge with you all. 44 * 孙夫人断后,挡住追兵 45 */ 46 public class BlockEnemy implements IStrategy { 47 public void operate() { 48 第 5 页 49 您的设计模式 50 System.out.println("孙夫人断后,挡住追兵"); 51 } 52 } 53 好了,大家看看,三个妙计是有了,那需要有个地方放这些妙计呀,放锦囊呀: 54 package com.cbf4life.strategy; 55 /** 56 * @author cbf4Life [email protected] 57 * I'm glad to share my knowledge with you all. 58 * 计谋有了,那还要有锦囊 59 */ 60 public class Context { 61 //构造函数,你要使用那个妙计 62 private IStrategy straegy; 63 public Context(IStrategy strategy){ 64 this.straegy = strategy; 65 } 66 67 //使用计谋了,看我出招了 68 public void operate(){ 69 this.straegy.operate(); 70 } 71 } 72 然后就是赵云雄赳赳的揣着三个锦囊,拉着已步入老年行列的、还想着娶纯情少女的、色迷迷的刘老 73 爷子去入赘了,嗨,还别说,小亮的三个妙计还真是不错,瞅瞅: 74 package com.cbf4life.strategy; 75 /** 76 * @author cbf4Life [email protected] 77 * I'm glad to share my knowledge with you all. 78 */ 79 public class ZhaoYun { 80 /** 81 * 赵云出场了,他根据诸葛亮给他的交代,依次拆开妙计 82 */ 83 public static void main(String[] args) { 84 Context context; 85 第 6 页 86 您的设计模式 87 88 //刚刚到吴国的时候拆第一个 89 System.out.println("-----------刚刚到吴国的时候拆第一个-------------"); 90 context = new Context(new BackDoor()); //拿到妙计 91 context.operate(); //拆开执行 92 System.out.println("\n\n\n\n\n\n\n\n"); 93 94 //刘备乐不思蜀了,拆第二个了 95 System.out.println("-----------刘备乐不思蜀了,拆第二个了-------------"); 96 context = new Context(new GivenGreenLight()); 97 context.operate(); //执行了第二个锦囊了 98 System.out.println("\n\n\n\n\n\n\n\n"); 99 100 //孙权的小兵追了,咋办?拆第三个 101 System.out.println("-----------孙权的小兵追了,咋办?拆第三个 102 -------------"); 103 context = new Context(new BlockEnemy()); 104 context.operate(); //孙夫人退兵 105 System.out.println("\n\n\n\n\n\n\n\n"); 106 107 /* 108 *问题来了:赵云实际不知道是那个策略呀,他只知道拆第一个锦囊, 109 *而不知道是BackDoor这个妙计,咋办? 似乎这个策略模式已经把计谋名称写出来了 110 * 111 * 错!BackDoor、GivenGreenLight、BlockEnemy只是一个代码,你写成first、second、 112 third,没人会说你错! 113 * 114 * 策略模式的好处就是:体现了高内聚低耦合的特性呀,缺点嘛,这个那个,我回去再查查 115 */ 116 } 117 }
首先一个接口,并有这个接口的几个实现类,在创建一个可以存放这些实现类的类(容器),类里存放这个接口,和方法并实现它的构造函数,还有一通过 接口调用实现类的方法
· 测试这个
获取容器类对象,容器类创建接口的不同实现类,在调用这个方法
不同实现类的不同就会返回不同的信息。
·总结 :测试时是通过接口进行获取类的实例对象所以降级了类之间的耦合
并提高了开发的可维护性(高内聚),(我们只需围绕着这个接口继续开发,而不用因为不同的实现而改变,比如list 它下面有不同的实现类)