策略模式

来源:互联网 发布:现在做淘宝店晚吗 编辑:程序博客网 时间:2024/05/22 12:16
策略模式  
刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按机拆开 解决棘手问题,嘿,还说,真是解决了大问题,搞到最后是周瑜陪了人又折兵呀,那咱 们先看看这个场景是什么样子的。 先说这个场景中的要素:三个妙计,一个锦囊,一个赵云,妙计是小亮同志给的,妙计 是放置在锦囊里,俗称就是锦囊妙计嘛,那赵云就是一个干活的人,从锦囊中取出妙计,执 行,然后获胜,用 JAVA 程序怎么表现这个呢? 三个妙计是同一类型的东东,那咱就写个接口: 
 
package com.cbf4life.strategy; 
 
/** 
 * @author cbf4Life cbf4life@126.com 
 * I'm glad to share my knowledge with you all.  * 首先定一个策略接口,这是诸葛亮老人家给赵云的三个锦囊妙计的接口 
 *  
 */ 
public interface IStrategy { 
   //每个锦囊妙计都是一个可执行的算法 
 public void operate(); 
 

 
然后再写三个实现类,有三个妙计嘛: 
  
package com.cbf4life.strategy; 
 
/** 
 * @author cbf4Life cbf4life@126.com 
 * I'm glad to share my knowledge with you all.  * 找乔国老帮忙,使孙权不能杀刘备 
 */ 
public class BackDoor implements IStrategy { 
 
 public void operate() {   System.out.println("找乔国老帮忙,让吴国给孙权施加压力"); 
 } 
 
}  
 
package com.cbf4life.strategy; 
 
/** 
 * @author cbf4Life cbf4life@126.com 
 * I'm glad to share my knowledge with you all.  * 求吴国开个绿灯 
 */ 
public class GivenGreenLight implements IStrategy { 
 
 
 public void operate() {   System.out.println("求吴国开个绿灯,放行!"); 
 } 
 

 
package com.cbf4life.strategy; 
 
/** 
 * @author cbf4Life cbf4life@126.com 
 * I'm glad to share my knowledge with you all.  * 孙人断后,挡住追兵 
 */ 
public class BlockEnemy implements IStrategy { 
 
 public void operate() {   System.out.println("孙人断后,挡住追兵"); 
 } 
 

 
好了,大家看看,三个妙计是有了,那需要有个地方放这些妙计呀,放锦囊呀:  
 
package com.cbf4life.strategy; 
 
/** 
 * @author cbf4Life cbf4life@126.com 
 * I'm glad to share my knowledge with you all.  * 计谋有了,那还要有锦囊 
 */ 
public class Context {  //构造函数,你要使用那个妙计 
 private IStrategy straegy; 
 public Context(IStrategy strategy){ 
  this.straegy = strategy; 
 } 
   //使用计谋了,看我出招了 
 public void operate(){ 
  this.straegy.operate(); 
 } 

 
然后就是赵云雄赳赳的揣着三个锦囊,拉着已步入老年行列的、还想着娶纯情少女的、 色迷迷的刘老爷子去入赘了,嗨,还说,小亮的三个妙计还真是不错,瞅瞅: 
 
package com.cbf4life.strategy; 
 
/** 
 * @author cbf4Life cbf4life@126.com 
 * I'm glad to share my knowledge with you all. 
 */ 
public class ZhaoYun { 
 
 /**   * 赵云出场了,他根据诸葛亮给他的交代,依次拆开妙计 
  */ 
 public static void main(String[] args) { 
  Context context; 
     //刚刚到吴国的时候拆第一个   System.out.println("-----------刚刚到吴国的时候拆第一个
-------------");   context = new Context(new BackDoor()); //拿到妙计   context.operate();  //拆开执行 
  System.out.println("\n\n\n\n\n\n\n\n");  
 
     //刘备乐不思蜀了,拆第二个了   System.out.println("-----------刘备乐不思蜀了,拆第二个了
-------------"); 
  context = new Context(new GivenGreenLight());   context.operate();  //执行了第二个锦囊了 
  System.out.println("\n\n\n\n\n\n\n\n"); 
     //孙权的小兵追了,咋办?拆第三个   System.out.println("-----------孙权的小兵追了,咋办?拆第三个
-------------"); 
  context = new Context(new BlockEnemy());   context.operate();  //孙人退兵 
  System.out.println("\n\n\n\n\n\n\n\n"); 
   
  /*    *问题来了:赵云实际不知道是那个策略呀,他只知道拆第一个锦囊,    *而不知道是BackDoor这个妙计,咋办?  似乎这个策略模式已经把计谋名称 写出来了 
   *    * 错!BackDoor、GivenGreenLight、BlockEnemy只是一个代码,你写成 first、second、third,没人会说你错! 
   *     * 策略模式的好处就是:体现了高内聚低耦合的特性呀,缺点嘛,这个那个,我 回去再查查 
   */ 
   
  
 } 
 

 
 就这三招,搞的周郎是“陪了人又折兵”呀!这就是策略模式,想要类图,baidu 上 google 一下,能查到一大堆策略模式的类图,纯技术的东西咱不说行不~ 

 
 
原创粉丝点击