原文来自:JAVA设计模式之禅完整版.pdf
public interface IStrategy {
//每个锦囊妙计都是一个可执行的算法
public void operate();
}
package com.cbf4life.strategy;
public class BackDoor implements IStrategy {
public void operate() {
System.out.println("找乔国老帮忙,让吴国太给孙权施加压力");
}
}
public class GivenGreenLight implements IStrategy {
public void operate() {
System.out.println("求吴国太开个绿灯,放行!");
}
}
public class BlockEnemy implements IStrategy {
public void operate() {
System.out.println("孙夫人断后,挡住追兵");
}
}
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,没人会说你错!
*
* 策略模式的好处就是:体现了高内聚低耦合的特性呀,缺点嘛,这个那个,我回去再查查
*/
}
}