- 策略类接口或抽象定义
public interface IStrategy {
void operate();
}
- 策略类接口或抽象定义实现类
public class ConcreteStrategy implements IStrategy {
@Override
public void operate() {
//具体逻辑处理
}
}
- 封装角色调用
public class Context {
private IStrategy strategy;
public Context(IStrategy strategy) {
this.strategy = strategy;
}
public void doSomething() {
this.strategy.operate();
}
}
- 场景使用
//具体策略对象
IStrategy strategy = new ConcreteStrategy();
//封装角色对象
Context context = new Context(strategy);
//执行
context.execute();
网友评论