设计模式策略模式

  • 实体类
package com.credi.util.handler;

import lombok.Data;

import java.math.BigDecimal;

@Data
public class Order {
    /**
     * 订单来源
     */
    private String source;
    /**
     * 支付方式
     */
    private String payMethod;
    /**
     * 订单编号
     */
    private String code;
    /**
     * 订单金额
     */
    private BigDecimal amount;
    // ...其他的一些字段
}

  • 接口规范
package com.credi.util.handler2;

import com.credi.util.handler.Order;

public interface OrderHandler {
    void handle(Order order);
}

  • 实现
package com.credi.util.handler3;

import com.credi.util.handler2.OrderHandlerType;

import java.lang.annotation.Annotation;

public class OrderHandlerTypeImpl implements OrderHandlerType {

    private String source;
    private String payMethod;

    OrderHandlerTypeImpl(String source, String payMethod) {
        this.source = source;
        this.payMethod = payMethod;
    }

    @Override
    public String source() {
        return source;
    }

    @Override
    public String payMethod() {
        return payMethod;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return OrderHandlerType.class;
    }

    // 因为以@OrderHandlerType注解为key  , 所以重写equals和hashcode方法
    @Override
    public int hashCode() {
        int hashCode = 0;
        hashCode += (127 * "source".hashCode()) ^ source.hashCode();
        hashCode += (127 * "payMethod".hashCode()) ^ payMethod.hashCode();
        return hashCode;
    }


    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof OrderHandlerType)) {
            return false;
        }
        OrderHandlerType other = (OrderHandlerType) obj;
        return source.equals(other.source()) && payMethod.equals(other.payMethod());
    }
}

  • 自定义注解
package com.credi.util.handler2;


import org.springframework.stereotype.Service;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
public @interface OrderHandlerType {
    String source();

    String payMethod();
}

  • 处理逻辑分支1
package com.credi.util.handler2;/**

import com.credi.util.handler.Order;

@OrderHandlerType(source = "mobile",payMethod = "微信")
public class MobileOrderHandler implements OrderHandler {
    @Override
    public void handle(Order order) {
        System.out.println("处理移动端订单" + ", 支付方式微信");
    }
}


  • 处理逻辑分支2
ackage com.credi.util.handler2;

import com.credi.util.handler.Order;

@OrderHandlerType(source = "pc",payMethod = "支付宝")
public class PCOrderHandler implements OrderHandler {
    @Override
    public void handle(Order order) {
        System.out.println("处理PC端订单" + ", 支付方式支付宝");
    }
}

  • 策略模式的使用
package com.credi.util.handler3;

import com.credi.util.handler.Order;
import com.credi.util.handler2.MobileOrderHandler;
import com.credi.util.handler2.OrderHandler;
import com.credi.util.handler2.OrderHandlerType;
import com.credi.util.handler2.PCOrderHandler;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class OrderService2 {

    private Map<OrderHandlerType, OrderHandler> orderHandleMap;

    /**
    * @Description: 载入处理方式
    */
    @Autowired
    public void setOrderHandleMap(List<OrderHandler> orderHandlers) {
        // 注入各种类型的订单处理类
        orderHandleMap = orderHandlers.stream().collect(
                Collectors.toMap(
                        orderHandler -> AnnotationUtils.findAnnotation(orderHandler.getClass(), OrderHandlerType.class), // 以元素的@OrderHandlerType注解为key  , OrderHandler
                        v -> v,  (v1, v2) -> v1) 
        );
    }

    /**
    * @Author: He Xingchi
    * @Description: 策略调用指定处理方式
    */
    public void orderService(Order order) {
        // ...一些前置处理

        // 通过订单来源确以及支付方式获取对应的handler
        OrderHandlerType orderHandlerType = new OrderHandlerTypeImpl(order.getSource(), order.getPayMethod());
        OrderHandler orderHandler = orderHandleMap.get(orderHandlerType);
        orderHandler.handle(order);

        // ...一些后置处理
    }

    /**
    * @Author: He Xingchi
    * @Description: 调用handler
    */
    public static void main(String[] args) {
        // 将支持的handler处理方式载入内存
        MobileOrderHandler mobileOrderHandler = new MobileOrderHandler();
        PCOrderHandler pcOrderHandler = new PCOrderHandler();
        List<OrderHandler> list = new ArrayList<>();
        list.add(mobileOrderHandler);
        list.add(pcOrderHandler);
        OrderService2 orderService = new OrderService2();
        orderService.setOrderHandleMap(list);

        // 策略调用handler
        Order order = new Order();
        order.setSource("mobile");
        order.setPayMethod("微信");
        order.setAmount(new BigDecimal("11"));

        orderService.orderService(order); // 微信处理逻辑 ... 

    }

    @Data
    @Accessors(chain = true)
    static class UserBo{
        private int UserId;
        private String UserName;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值