头图

The strategy is the encapsulation of algorithms. Different algorithms are encapsulated into independent classes with the same interface, so that the algorithm itself and the client are separated, and the algorithms can be replaced with each other.

There are mainly the following roles in the strategy mode

  • Context information class (Context): used to store and execute the specific strategy class that needs to be used and the logic invoked by the client.
  • Abstract Strategy Class (Strategy): Common methods for defining strategies.
  • Concrete strategy classes (ConcreteStrategy, etc.): Implement common methods defined by abstract strategy classes.

The following uses the strategy mode to simulate a scenario in my development process. In the aggregation payment, merchants often bind some boxes specially used for payment broadcasts, which we call cloud broadcasts. These cloud broadcasts may be produced by different manufacturers. We When making notifications, because the interfaces and calling methods given by manufacturers are also different, we abstract the cloud broadcast notification into an abstract strategy, and each specific manufacturer is a specific strategy.

abstract strategy

 public interface CloudHornStrategy {
    /**
     * 发送消息
     * @param body
     * @return
     */
    boolean sendMessage(CloudRequestBody body);
}

parameter class

 public class CloudRequestBody {
    private static Map<Integer,String> channelMap = new HashMap<>();
    static {
        channelMap.put(1,"支付宝");
        channelMap.put(2,"微信");
        channelMap.put(3,"云闪付");
        channelMap.put(4,"数字人民币");
    }
    /**
     * 金额
     */
    private Long money;
    /**
     * 通道
     */
    private String channel;

    public CloudRequestBody(Long money,int channel) {
        this.money = money;
        this.channel = channelMap.get(channel);
    }

    public Long getMoney() {
        return money;
    }

    public String getChannel() {
        return channel;
    }
}

specific strategies

 public class LDCloudHornStrategy implements CloudHornStrategy{
    @Override
    public boolean sendMessage(CloudRequestBody body) {
        //.....
        //一些列组装参数,数据处理,调用厂商接口
        String msg = String.format("LD厂商推送:%s到账%d元", body.getChannel(), body.getMoney());
        System.out.println(msg);
        return true;
    }
}
 @Override
    public boolean sendMessage(CloudRequestBody body) {
        //.....
        //一些列组装参数,数据处理,调用厂商接口
        String msg = String.format("TY厂商推送:%s到账%d元", body.getChannel(), body.getMoney());
        System.out.println(msg);
        return true;
    }
}

notification context

 public class PayNotifyContext {
    private static Map<String,CloudHornStrategy> strategyMap = null;
    static {
        strategyMap = new HashMap<>();
        strategyMap.put("LD",new LDCloudHornStrategy());
        strategyMap.put("TY",new TYCloudHornStrategy());
    }
    private CloudHornStrategy strategy;

    public PayNotifyContext(String horn) {
        this.strategy = strategyMap.get(horn);
    }

    public void payNotify(CloudRequestBody body){
        strategy.sendMessage(body);
    }
}

Payment Client Notification

 public class PayClient {

    public static void main(String[] args) {
        //支付成功,回调通知接口
        //获取用户绑定了几个云播报分别是什么类型
        //比如说用户有三个云播报....当然获取的用户数据不可能只有用户用的云播报品牌,其它用户信息我们暂且不管
        String[] horns = new String[]{"TY","LD","LD"};
        //假设支付了误元
        CloudRequestBody body = new CloudRequestBody(5L,1);
        for (String horn:horns) {
            new PayNotifyContext(horn).payNotify(body);
        }
    }
}
=====结果=====
TY厂商推送:支付宝到账5元
LD厂商推送:支付宝到账5元
LD厂商推送:支付宝到账5元

When we use a new vendor's cloud broadcast, we only need to add a strategy, and then add an enumeration value to the corresponding set, so that the code changes are minimal, in line with the open-closed principle, and greatly decoupled.


eacape
205 声望8 粉丝

JAVA 攻城狮


« 上一篇
模板方法模式
下一篇 »
状态模式