天天看点

第 1 章 策略模式【Strategy Pattern】

刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手问题,

嘿,还别说,真是解决了大问题,搞到最后是周瑜陪了夫人又折兵呀,那咱们先看看这个场景是什么样子

的。

先说这个场景中的要素:三个妙计,一个锦囊,一个赵云,妙计是小亮同志给的,妙计是放置在锦囊

里,俗称就是锦囊妙计嘛,那赵云就是一个干活的人,从锦囊中取出妙计,执行,然后获胜,用JAVA 程序

怎么表现这个呢?我们先看类图:

第 1 章 策略模式【Strategy Pattern】

三个妙计是同一类型的东东,那咱就写个接口:

package com.cbf4life.strategy;

/**

* @author cbf4Life ​​​[email protected]​​​ * I'm glad to share my knowledge with you all.

* 首先定一个策略接口,这是诸葛亮老人家给赵云的三个锦囊妙计的接口

*

*/

public interface IStrategy {

//每个锦囊妙计都是一个可执行的算法

public void operate();

}

然后再写三个实现类,有三个妙计嘛:

package com.cbf4life.strategy;

/**

* @author cbf4Life

​​[email protected]​​

* 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

​​[email protected]​​

* 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

​​[email protected]​​

* I'm glad to share my knowledge with you all.

* 孙夫人断后,挡住追兵

*/

public class BlockEnemy implements IStrategy {

public void operate() {

您的设计模式

第 6 页

System.out.println("孙夫人断后,挡住追兵");

}

}

好了,大家看看,三个妙计是有了,那需要有个地方放这些妙计呀,放锦囊呀:

package com.cbf4life.strategy;

/**

* @author cbf4Life

​​[email protected]​​

* 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

​​[email protected]​​

* I'm glad to share my knowledge with you all.

*/

public class ZhaoYun {

/**

* 赵云出场了,他根据诸葛亮给他的交代,依次拆开妙计

*/

public static void main(String[] args) {

Context context;

您的设计模式

第 7 页

//刚刚到吴国的时候拆第一个

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,没人会说你错!

*

* 策略模式的好处就是:体现了高内聚低耦合的特性呀,缺点嘛,这个那个,我回去再查查

*/

}

}

就这三招,搞的周郎是“陪了夫人又折兵”呀!这就是策略模式,高内聚低耦合的特点也表现出来了,

还有一个就是扩展性,也就是OCP 原则,策略类可以继续增加下去,只要修改Context.java 就可以了,这

继续阅读