[Design Mode] - 14, Strategy Mode

(14) Strategy Mode

The strategy pattern refers to the encapsulation so that each algorithm or component can be replaced and combined with each other, and different strategies can be selected to deal with problems flexibly in different scenarios.

1. The design principles of the strategy pattern

Context: The context environment used to operate the strategy and shield the direct access of the high-level module to the algorithm;

Abstract Strategy (IStragey): The top-level interface of a strategy or algorithm;

ConcreteStrategy: Concrete strategy.

image-20210314093032517

2. Simple example

In payment scenarios, there are usually multiple payment methods to choose from, and the strategy mode is used to manage multiple payment strategies.

public abstract class Payment {
    
    

    /**
     * 抽象方法,子类需要重写,获得实现类方法名称
     *
     * @return
     */
    public abstract String getName();

    public MsgResult pay(String uid, double amout) {
    
    
        if (queryBalance(uid) < amout) {
    
    
            return new MsgResult("余额不足!", 500, "支付失败!");
        }
        return new MsgResult("支付成功!", 200, "支付金额" + amout);
    }

    protected abstract double queryBalance(String uid);

}
public class AliPay extends Payment {
    
    
    @Override
    public String getName() {
    
    
        return "支付宝";
    }

    @Override
    protected double queryBalance(String uid){
    
    
        return 1000;
    }
}
public class JDPay extends Payment{
    
    
    @Override
    public String getName() {
    
    
        return "京东白条";
    }

    @Override
    protected double queryBalance(String uid) {
    
    
        return 200;
    }
}
public class WechatPay extends Payment {
    
    
    @Override
    public String getName() {
    
    
        return "微信支付";
    }

    @Override
    protected double queryBalance(String uid) {
    
    
        return 600;
    }
}
public class MsgResult {
    
    
    private String msg;
    private int code;
    private Object data;

    public MsgResult(String msg, int code, Object data) {
    
    
        this.msg = msg;
        this.code = code;
        this.data = data;
    }
    //getter,setter
}
public class Order {
    
    
    private String uid;
    private String orderId;
    private double amount;

    public Order(String uid, String orderId, double amount) {
    
    
        this.uid = uid;
        this.orderId = orderId;
        this.amount = amount;
    }

    //    方法重载
    public MsgResult pay() {
    
    
        return pay(PayStrategy.DEFAULT_PAY);
    }

    public MsgResult pay(String payKey) {
    
    
        Payment payment = PayStrategy.get(payKey);
        System.out.println("欢迎使用" + payment.getName());
        System.out.println("本次交易金额" + amount + ",开始扣款");
        return payment.pay(uid, amount);
    }
}
/**
 * 支付策略管理
 */
public class PayStrategy {
    
    
    public static final String ALI_PAY = "AliPay";
    public static final String JD_PAY = "JdPay";
    public static final String WECHAT_PAY = "WechatPay";
    public static final String DEFAULT_PAY = "AliPay";

    private static Map<String, Payment> strategy = new HashMap<>();

    static {
    
    
        strategy.put(ALI_PAY, new AliPay());
        strategy.put(JD_PAY, new JDPay());
        strategy.put(WECHAT_PAY, new WechatPay());
    }

    public static Payment get(String payKey) {
    
    
        if (!strategy.containsKey(payKey)) {
    
    
            return strategy.get(DEFAULT_PAY);
        }
        return strategy.get(payKey);
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Order order = new Order("1", "202103110007", 666);
//        通过构造方法抽象策略管理context灵活选择具体策略
        System.out.println(order.pay(PayStrategy.WECHAT_PAY));
    }
}
image-20210314094559306

3. Evaluation of the strategy model

The strategy mode conforms to the principle of opening and closing, provides a management method for algorithm groups, and can flexibly combine algorithms. The disadvantage is that the client needs to know all the policies, and the system maintenance burden increases.

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118054485