工厂模式又称工厂方法模式,是一种创建型设计模式,其在父类中提供一个创建对象的方法,允许子类觉得实例化对象的类型。

案例思维导图

工厂方法模式 - 图1

奖品发放案例

  1. public interface ICommodity {
  2. void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception;
  3. }
  1. public class GoodsCommodityService implements ICommodity {
  2. private GoodsService goodsService = new GoodsService();
  3. public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
  4. DeliverReq deliverReq = new DeliverReq();
  5. deliverReq.setUserName(queryUserName(uId));
  6. deliverReq.setUserPhone(queryUserPhoneNumber(uId));
  7. deliverReq.setSku(commodityId);
  8. deliverReq.setOrderId(bizId);
  9. deliverReq.setConsigneeUserName(extMap.get("consigneeUserName"));
  10. deliverReq.setConsigneeUserPhone(extMap.get("consigneeUserPhone"));
  11. deliverReq.setConsigneeUserAddress(extMap.get("consigneeUserAddress"));
  12. Boolean isSuccess = goodsService.deliverGoods(deliverReq);
  13. logger.info("请求参数[实物商品] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
  14. logger.info("测试结果[实物商品]:{}", isSuccess);
  15. if (!isSuccess) throw new RuntimeException("实物商品发放失败");
  16. }
  17. private String queryUserName(String uId) {
  18. return "花花";
  19. }
  20. private String queryUserPhoneNumber(String uId) {
  21. return "15200101232";
  22. }
  23. }
  1. public class CouponCommodityService implements ICommodity {
  2. private CouponService couponService = new CouponService();
  3. public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
  4. CouponResult couponResult = couponService.sendCoupon(uId, commodityId, bizId);
  5. logger.info("请求参数[优惠券] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
  6. logger.info("测试结果[优惠券]:{}", JSON.toJSON(couponResult));
  7. if (!"0000".equals(couponResult.getCode())) throw new RuntimeException(couponResult.getInfo());
  8. }
  9. }
  1. public class CardCommodityService implements ICommodity {
  2. // 模拟注入
  3. private IQiYiCardService iQiYiCardService = new IQiYiCardService();
  4. public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
  5. String mobile = queryUserMobile(uId);
  6. iQiYiCardService.grantToken(mobile, bizId);
  7. logger.info("请求参数[爱奇艺兑换卡] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
  8. logger.info("测试结果[爱奇艺兑换卡]:success");
  9. }
  10. private String queryUserMobile(String uId) {
  11. return "15200101232";
  12. }
  13. }
  1. public class StoreFactory {
  2. /**
  3. * 奖品状态类型方式实例化
  4. * @param commodityType 奖品类型
  5. * @return 实例化对象
  6. */
  7. public ICommodity getCommodityService(Integer commodityType) {
  8. if (null == commodityType) return null;
  9. if (1 == commodityType) return new CouponCommodityService();
  10. if (2 == commodityType) return new GoodsCommodityService();
  11. if (3 == commodityType) return new CardCommodityService();
  12. throw new RuntimeException("不存在的奖品服务类型");
  13. }
  14. /**
  15. * 奖品状态方式实例化
  16. * @param clazz 奖品类
  17. * @return 实例化对象
  18. */
  19. public ICommodity getCommodityService(Class<? extends ICommodity> clazz) throws IllegalAccessException, InstantiationException {
  20. if (null == clazz) return null;
  21. return clazz.newInstance();
  22. }
  23. }