• 工厂方法模式(模拟发奖多种商品)


    目录

    定义

    模拟发奖多种商品

    优惠券

    实物商品

    第三⽅爱奇艺兑换卡

    代码实现

    定义发奖接⼝

    实现奖品发放接⼝

    优惠券

    实物商品

    第三⽅兑换卡

    创建商店⼯⼚

    测试验证


    定义

    在⽗类中提供⼀个创建对象的⽅法, 允许⼦类决定实例化对象的类型。

    模拟发奖多种商品

    在这⾥我们模拟积分兑换中的发放多种类型商品,假如现在我们有如下三种类型的商品接⼝

    优惠券

    CouponResult sendCoupon(String uId, String couponNumber, String uuid)

    实物商品

    Boolean deliverGoods(DeliverReq req)

    第三⽅爱奇艺兑换卡

    void grantToken(String bindMobileNumber, String cardId)

    代码实现

    定义发奖接⼝

    1. public interface ICommodity {
    2. void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception;
    3. }

    实现奖品发放接⼝

    优惠券

    1. public class CouponCommodityService implements ICommodity {
    2. private Logger logger = LoggerFactory.getLogger(CouponCommodityService.class);
    3. private CouponService couponService = new CouponService();
    4. public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
    5. CouponResult couponResult = couponService.sendCoupon(uId, commodityId, bizId);
    6. logger.info("请求参数[优惠券] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
    7. logger.info("测试结果[优惠券]:{}", JSON.toJSON(couponResult));
    8. if (!"0000".equals(couponResult.getCode())) throw new RuntimeException(couponResult.getInfo());
    9. }
    10. }

    实物商品

    1. public class GoodsCommodityService implements ICommodity {
    2. private Logger logger = LoggerFactory.getLogger(GoodsCommodityService.class);
    3. private GoodsService goodsService = new GoodsService();
    4. public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
    5. DeliverReq deliverReq = new DeliverReq();
    6. deliverReq.setUserName(queryUserName(uId));
    7. deliverReq.setUserPhone(queryUserPhoneNumber(uId));
    8. deliverReq.setSku(commodityId);
    9. deliverReq.setOrderId(bizId);
    10. deliverReq.setConsigneeUserName(extMap.get("consigneeUserName"));
    11. deliverReq.setConsigneeUserPhone(extMap.get("consigneeUserPhone"));
    12. deliverReq.setConsigneeUserAddress(extMap.get("consigneeUserAddress"));
    13. Boolean isSuccess = goodsService.deliverGoods(deliverReq);
    14. logger.info("请求参数[优惠券] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
    15. logger.info("测试结果[优惠券]:{}", isSuccess);
    16. if (!isSuccess) throw new RuntimeException("实物商品发放失败");
    17. }
    18. private String queryUserName(String uId) {
    19. return "花花";
    20. }
    21. private String queryUserPhoneNumber(String uId) {
    22. return "15200101232";
    23. }
    24. }

    第三⽅兑换卡

    1. public class CardCommodityService implements ICommodity {
    2. private Logger logger = LoggerFactory.getLogger(CardCommodityService.class);
    3. // 模拟注⼊
    4. private IQiYiCardService iQiYiCardService = new IQiYiCardService();
    5. public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
    6. String mobile = queryUserMobile(uId);
    7. iQiYiCardService.grantToken(mobile, bizId);
    8. logger.info("请求参数[爱奇艺兑换卡] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
    9. logger.info("测试结果[爱奇艺兑换卡]:success");
    10. }
    11. private String queryUserMobile(String uId) {
    12. return "15200101232";
    13. }
    14. }

    创建商店⼯⼚

    1. public class StoreFactory {
    2. public ICommodity getCommodityService(Integer commodityType) {
    3. if (null == commodityType) return null;
    4. if (1 == commodityType) return new CouponCommodityService();
    5. if (2 == commodityType) return new GoodsCommodityService();
    6. if (3 == commodityType) return new CardCommodityService();
    7. throw new RuntimeException("不存在的商品服务类型");
    8. }
    9. }

    测试验证

    1. @Test
    2. public void test_commodity() throws Exception {
    3. StoreFactory storeFactory = new StoreFactory();
    4. // 1. 优惠券
    5. ICommodity commodityService_1 = storeFactory.getCommodityService(1);
    6. commodityService_1.sendCommodity("10001", "EGM1023938910232121323432", "791098764902132", null);
    7. // 2. 实物商品
    8. ICommodity commodityService_2 = storeFactory.getCommodityService(2);
    9. Map<String,String> extMap = new HashMap<String,String>();
    10. extMap.put("consigneeUserName", "方朋友");
    11. extMap.put("consigneeUserPhone", "15200292123");
    12. extMap.put("consigneeUserAddress", "吉林省.⻓春市.双阳区.XX街道.xx⼩区.#18-2109");
    13. commodityService_2.sendCommodity("10001","98198721311","1023020112221113", extMap);
    14. // 3. 第三⽅兑换卡(爱奇艺)
    15. ICommodity commodityService_3 = storeFactory.getCommodityService(3);
    16. commodityService_3.sendCommodity("10001","AQY1xjkUodl8LO975GdfrYUio",null ,null);
    17. }
  • 相关阅读:
    【PAT甲级】1077 Kuchiguse
    Exch:修复丢失的系统邮箱
    springboot+vue+elementUI大学生体质测试管理系统#毕业设计
    【Tomcat目录详解】关于Tomcat你还需要了解的详细内容
    Linux中进程管理
    Angular 7 升级到Angular 12
    医学之细胞组织基因(完整)
    CSS期末复习速览(二)
    偶数N 马蹄集
    香港玉湖集团亮相进博会——展现实力风采,建立共赢平台
  • 原文地址:https://blog.csdn.net/fanghailiang2016/article/details/138200101