“策略模式”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的1个中间版本) | |||
第123行: | 第123行: | ||
public void doSomething() { | public void doSomething() { | ||
System.out.println("具体策略2的运算法则"); | System.out.println("具体策略2的运算法则"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight>Context.java<syntaxhighlight lang="java"> | ||
+ | /** | ||
+ | * 封装角色 | ||
+ | */ | ||
+ | public class Context { | ||
+ | |||
+ | //抽象策略 | ||
+ | private Strategy strategy = null; | ||
+ | |||
+ | //构造函数设置具体策略 | ||
+ | public Context(Strategy _strategy) { | ||
+ | this.strategy = _strategy; | ||
+ | } | ||
+ | |||
+ | //封装后的策略方法 | ||
+ | public void doAnything() { | ||
+ | this.strategy.doSomething(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight>Client.java<syntaxhighlight lang="java"> | ||
+ | public class Client { | ||
+ | |||
+ | /** | ||
+ | * @param args | ||
+ | */ | ||
+ | public static void main(String[] args) { | ||
+ | //声明一个具体策略 | ||
+ | Strategy strategy = new ConcreteStrategy1(); | ||
+ | //声明上下文对象 | ||
+ | Context context = new Context(strategy); | ||
+ | //执行封装后的方法 | ||
+ | context.doAnything(); | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
2024年7月9日 (二) 13:10的最新版本
IStrategy.java
public interface IStrategy {
public void operate();
}
BackDoor.java
/**
* 乔国老开后门
*/
public class BackDoor implements IStrategy {
@Override
public void operate() {
System.out.println("找乔国老帮忙,让吴国太给孙权施加压力");
}
}
GivenGreenLight.java
/**
* 吴国太开绿灯
*/
public class GivenGreenLight implements IStrategy {
@Override
public void operate() {
System.out.println("求吴国太开绿灯,放行!");
}
}
BlockEnemy.java
/**
* 孙夫人断后
*/
public class BlockEnemy implements IStrategy {
@Override
public void operate() {
System.out.println("孙夫人断后,挡住追兵");
}
}
Context.java
/**
* 锦囊
*/
public class Context {
private IStrategy strategy;
public Context(IStrategy strategy) {
this.strategy = strategy;
}
public void operate() {
this.strategy.operate();
}
}
ZhaoYun.java
public class ZhaoYun {
/**
* @param args
*/
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");
System.out.println("---刘备乐不思蜀了,拆第二个了---");
context = new Context(new GivenGreenLight()); //拿到妙计
context.operate(); //执行了第二个锦囊
System.out.println("\n\n\n\n\n\n");
System.out.println("---孙权的小兵追来了,咋办?拆第三个---");
context = new Context(new BlockEnemy());
context.operate();
System.out.println("\n\n\n\n\n");
}
}
策略模式的定义
Define a family of algorithms, encapsulate each one, and make them interchangeable.
定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。
策略模式使用的就是面向对象的继承和多态机制,策略模式主要有三个角色:
- Context 封装角色,也叫作上下文角色,起承上启下封装作用,屏蔽高层模块对策略、算法的直接访问,封装可能存在的变化。
- Strategy 抽象策略角色,策略、算法家族的抽象,通常为接口,定义每个策略或算法必须具有的方法和属性。
- ConcreteStrategy 具体策略角色,实现抽象策略中的操作,该类还有具体的算法。
策略模式的通用源码
Strategy.java
public interface Strategy {
//策略模式的运算法则
public void doSomething();
}
ConcreteStrategy1.java
public class ConcreteStrategy1 implements Strategy {
@Override
public void doSomething() {
System.out.println("具体策略1的运算法则");
}
}
ConcreteStrategy2.java
public class ConcreteStrategy2 implements Strategy {
@Override
public void doSomething() {
System.out.println("具体策略2的运算法则");
}
}
Context.java
/**
* 封装角色
*/
public class Context {
//抽象策略
private Strategy strategy = null;
//构造函数设置具体策略
public Context(Strategy _strategy) {
this.strategy = _strategy;
}
//封装后的策略方法
public void doAnything() {
this.strategy.doSomething();
}
}
Client.java
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
//声明一个具体策略
Strategy strategy = new ConcreteStrategy1();
//声明上下文对象
Context context = new Context(strategy);
//执行封装后的方法
context.doAnything();
}
}