策略模式

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

你可能感兴趣的:(策略模式)