策略模式的实现

方式一

定义策略名称

public class MsgTypeConstant {

    public static final String chat_msg = "chatMsg";
    public static final String sys_msg = "sysMsg";
}

定义策略名称注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Type {
    String value();
}

定义策略行为接口

public interface BaseMsgHandler {
    void handleMsg(String content);
}

定义策略处理器

@Component
@Type(value = MsgType.CHAT_MSG)
public class ChatMsgHandler implements BaseMsgHandler{

    @Override
    public void handleMsg(String msg) {
        System.out.println("This is chatMsg. Detail msg information is :" + msg);
    }
}
@Component
@Type(value = MsgType.SYS_MSG)
public class SysMsgHandler implements BaseMsgHandler{

    @Override
    public void handleMsg(String msg) {
        System.out.println("This is sysMsg. Detail msg information is :" + msg);
    }
}

初始化策略

@Component
public class MsgConfig implements ApplicationContextAware {

    public static final Map<String, BaseMsgHandler> msgStrategyMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansWithAnnotation(Type.class).entrySet().iterator().forEachRemaining(entrySet ->{

            msgStrategyMap.put(entrySet.getValue().getClass().getAnnotation(Type.class).value(),
                    (BaseMsgHandler) entrySet.getValue());
        });
    }
}

定义实体类 接口

@Data
public class Msg{
    private String content;
    private MsgTypeConstant msgType;
}
	@ApiOperation(value="msg")
	@RequestMapping("msg")
	public void handleMsg(@RequestParam(name="content") String content, @RequestParam(name="msgType") String msgType){
		BaseMsgHandler handler = MsgConfig.msgStrategyMap.get(msgType);
		handler.handleMsg(content);
	}

方式二

private OrderService orderService;

@PostMapping("/makeOrder")
// 商品id
// 支付类型
public ResultData makeOrder(Long goodsId,String type){
    // 生成本地的订单
    Order order = this.orderService.makeOrder(goodsId);
    //选择支付方式
    PayType payType = PayType.getByCode(type);
    //进行支付
    payType.get().pay(order.getId(),order.getAmount());
    return this.ok();
}
public enum PayType {
    //支付宝        AliPay 是实现类
    ALI_PAY("1",new AliPay()),
    //微信
    WECHAT_PAY("2",new WechatPay());

    private String payType;
    // 这是一个接口
    private Payment payment;
    PayType(String payType,Payment payment){
        this.payment = payment;
        this.payType = payType;
    }
    //通过get方法获取支付方式
    public Payment get(){ return  this.payment;}
    
    public static PayType getByCode(String payType) {
        for (PayType e : PayType.values()) {
            if (e.payType.equals(payType)) {
                return e;
            }
        }
        return null;
    }
}
public interface Payment {
    public void pay(Long order, double amount);
}
public class AliPay implements Payment {
    @Override
    public void pay(Long order, double amount) {
        System.out.println("----支付宝支付----");
        System.out.println("支付宝支付111元");
    }
}
public class WechatPay implements Payment {
    @Override
    public void pay(Long orderId, double amount) {
        System.out.println("---微信支付---");
        System.out.println("支付222元");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值