package 策略模式;
public interface IStrategy {
public void operate();
}
//**************************************************************************
package 策略模式;
public class BackDoor implements IStrategy {
public void operate() {
System.out.println("找乔国老帮忙,让吴国老给孙权施加压力···");
}
}
//**************************************************************************
package 策略模式;
public class GivenGreenLight implements IStrategy {
public void operate() {
System.out.println("求吴国老给开灯,放行····");
}
}
//**************************************************************************
package 策略模式;
public class BlockEnemy implements IStrategy {
public void operate() {
System.out.println("孙夫人断后,挡住追兵···");
}
}
//**************************************************************************
package 策略模式;
public class Context {
private IStrategy strategy;
public Context(IStrategy strategy){
this.strategy = strategy;
}
public void operate(){
this.strategy.operate();
}
}
//**************************************************************************
package 策略模式;
public class ZhaoYun {
public static void main(String args[]){
Context context;
System.out.println("刚刚到吴国的时候,折开第一个···");
context = new Context(new BackDoor());
context.operate();
System.out.println("刘备乐不思蜀,于是折开第二个···");
context = new Context(new GivenGreenLight());
context.operate();
System.out.println("孙权的小兵追了,于是折开第三个···");
context = new Context(new BlockEnemy());
context.operate();
}
}
//**************************************************************************
策略模式
最新推荐文章于 2024-11-12 21:14:07 发布