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

目录

定义

模拟发奖多种商品

优惠券

实物商品

第三⽅爱奇艺兑换卡

代码实现

定义发奖接⼝

实现奖品发放接⼝

优惠券

实物商品

第三⽅兑换卡

创建商店⼯⼚

测试验证


定义

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

模拟发奖多种商品

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

优惠券

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

实物商品

Boolean deliverGoods(DeliverReq req)

第三⽅爱奇艺兑换卡

void grantToken(String bindMobileNumber, String cardId)

代码实现

定义发奖接⼝

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

实现奖品发放接⼝

优惠券

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

实物商品

public class GoodsCommodityService implements ICommodity {
    private Logger logger = LoggerFactory.getLogger(GoodsCommodityService.class);
    private GoodsService goodsService = new GoodsService();
    public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
        DeliverReq deliverReq = new DeliverReq();
        deliverReq.setUserName(queryUserName(uId));
        deliverReq.setUserPhone(queryUserPhoneNumber(uId));
        deliverReq.setSku(commodityId);
        deliverReq.setOrderId(bizId);
        deliverReq.setConsigneeUserName(extMap.get("consigneeUserName"));
        deliverReq.setConsigneeUserPhone(extMap.get("consigneeUserPhone"));
        deliverReq.setConsigneeUserAddress(extMap.get("consigneeUserAddress"));
        Boolean isSuccess = goodsService.deliverGoods(deliverReq);
        logger.info("请求参数[优惠券] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
        logger.info("测试结果[优惠券]:{}", isSuccess);
        if (!isSuccess) throw new RuntimeException("实物商品发放失败");
    }
    
    private String queryUserName(String uId) {
        return "花花";
    }
    
    private String queryUserPhoneNumber(String uId) {
        return "15200101232";
    }
}

第三⽅兑换卡

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

创建商店⼯⼚

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

测试验证

@Test
public void test_commodity() throws Exception {
    StoreFactory storeFactory = new StoreFactory();
    // 1. 优惠券
    ICommodity commodityService_1 = storeFactory.getCommodityService(1);
    commodityService_1.sendCommodity("10001", "EGM1023938910232121323432", "791098764902132", null);
     
    // 2. 实物商品
    ICommodity commodityService_2 = storeFactory.getCommodityService(2);
    Map<String,String> extMap = new HashMap<String,String>();
    extMap.put("consigneeUserName", "方朋友");
    extMap.put("consigneeUserPhone", "15200292123");
    extMap.put("consigneeUserAddress", "吉林省.⻓春市.双阳区.XX街道.xx⼩区.#18-2109");
    commodityService_2.sendCommodity("10001","98198721311","1023020112221113", extMap);
 
    // 3. 第三⽅兑换卡(爱奇艺)
    ICommodity commodityService_3 = storeFactory.getCommodityService(3);
    commodityService_3.sendCommodity("10001","AQY1xjkUodl8LO975GdfrYUio",null ,null);
}

相关推荐

  1. 工厂方法模式模拟发奖多种商品

    2024-04-26 09:42:03       32 阅读
  2. 设计模式-工厂方法模式

    2024-04-26 09:42:03       62 阅读
  3. 设计模式-工厂方法模式

    2024-04-26 09:42:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 09:42:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 09:42:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 09:42:03       82 阅读
  4. Python语言-面向对象

    2024-04-26 09:42:03       91 阅读

热门阅读

  1. Elasticsearch索引别名:管理与优化数据访问

    2024-04-26 09:42:03       38 阅读
  2. log4j:WARN No appenders could be found for logger

    2024-04-26 09:42:03       34 阅读
  3. Ubuntu离线安装g++、locales

    2024-04-26 09:42:03       29 阅读
  4. Circuits--Sequential--Finite_2

    2024-04-26 09:42:03       30 阅读
  5. centos7 宝塔php7安装mongodb扩展

    2024-04-26 09:42:03       33 阅读
  6. 密码学系列0-总述

    2024-04-26 09:42:03       28 阅读
  7. mysql服务器无法启动问题处理

    2024-04-26 09:42:03       33 阅读
  8. 图神经网络 | Pytorch图神经网络ST-GNN

    2024-04-26 09:42:03       28 阅读
  9. Django项目之图书管理系统

    2024-04-26 09:42:03       23 阅读
  10. 中文语音识别实战(ASR)

    2024-04-26 09:42:03       28 阅读
  11. Spring

    Spring

    2024-04-26 09:42:03      29 阅读