设计模式——工厂模式
工厂模式
1.需求
抽奖活动
新用户注册即参与抽奖,奖品为:1.优惠券,2.优酷会员,3.京东卡
2.无模式
public class FreeGoodsController{public Result<?> send(AwardInfo awardInfo){int awardType = awardInfo.getAwardType();if(awardType == 1){DiscountService discountService = new DiscountService();discountService.send();return Result.success();}if(awardType == 2){YouKuService youkuService = new YouKuService;youkuService.send();return Result.success();}if(awardType == 3){JDService jdService = new JDService();jdService.send();return Result.success();}return Result.failed();}}
3.简单工厂模式
简单工厂模式包含以下特征:
- 抽象产品
- 具体产品
- 具体工厂
3.1抽象产品
public interface IFreeGoods{boolean send();
}
3.2具体产品
public class DiscountService implements IFreeGoods{public boolean send(){System.out.println("发放打折券");return true;}
}
public class YouKuService implements IFreeGoods{public boolean send(){System.out.println("发放优酷会员");return true;}
}
public class JDCardService implements IFreeGoods{public boolean send(){System.out.println("发放京东卡");return true;}
}
3.3具体工厂
public class FreeGoodsServiceFactory{public static IFreeGoods getInstance(Integer awardType){if(awardType == 1){return new DiscountService();}if(awardType == 2){return new YouKuService();}if(awardType == 3){return new JDCardService();}return null;}
}
public class FreeGoodsController{public Result<?> send(AwardInfo awardInfo){int awardType = awardInfo.getAwardType();IFreeGoods freeGoods = FreeGoodsFServiceFactory.getInstance(awardType);if(freeGoods.send()){return Result.success();}else {return Result.failed();}}}
4.工厂方法模式
- 抽象产品
- 具体产品
- 抽象工厂
- 具体工厂
4.1抽象产品——定义产品的行为
public interface IFreeGoods{boolean send();
}
4.2具体产品——实现或继承抽象产品的子类
public DiscountFreeGoods implements IFreeGoods{public boolean send(){System.out.println("发放打折券");return true;}
}
public YouKuFreeGoods implements IFreeGoods{public boolean send(){System.out.println("发放优酷会员");return true;}
}
public JDFreeGoods implements IFreeGoods{public boolean send(){System.out.println("发放京东卡");return true;}
}
4.3抽象工厂——提供创建产品的接口
public interface FreeGoodsFactory{IFreeGoods createFreeGoods();
}
4.4具体工厂
public class DiscountFreeGoodsFactory implements FreeGoodsFacotry{public IFreeGoods createFreeGoods(){return new DiscountFreeGoods();}
}
public class YouKuFreeGoodsFactory implements FreeGoodsFacotry{public IFreeGoods createFreeGoods(){return new YoukuFreeGoods();}
}
public class JDFreeGoodsFactory implements FreeGoodsFacotry{public IFreeGoods createFreeGoods(){return new JDFreeGoods();}
}
5.抽象工厂模式
基于上述拉新人注册的需求进行扩展
之前是的奖品为:平台的打折券、优酷的月卡、京东的小礼品
现在的奖品为:平台的打折券、平台的会员卡,优酷的月卡打折券、优酷的月卡,京东的商品打折券、京东会员卡
由现在的需求可以看出,打折券、会员卡可以归为一类,奖品提供方可以归为另一类
打折券发放以后可以购买相关平台的指定商品
会员卡发放以后需要绑定相关平台的用户账户
由此引出了产品族以及产品等级结构的概念
5.1产品等级结构
产品等级结构即产品的继承结构,如一个抽象类是打折券,其子类有平台的打折券、优酷的打折券、京东的打折券;
5.2产品族
产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品,比如平台的工厂生产平台的打折券,生产平台的会员卡
5.3抽象工厂模式的具体特征
- 抽象产品(产品等级结构父类)
- 具体产品(产品等级结构子类)
- 抽象工厂(产品族父类)
- 具体工厂(产品族子类)
5.4抽象产品
public interface IFreeGoods{boolean send();
}
public abstract class DiscountFreeGoods extends IFreeGoods{// 获取售卖的商品List getSaleList();// 计算价格BigDecimal calc(BigDecimal price);
}
public abstract class VipCardFreeGoods extends IFreeGoods {boolean bindAccount(Long phoneNumber);
}
5.5抽象工厂
public interface FreeGoodsFactory{DiscountFreeGoods createDiscountFreeGoods();VipCardFreeGoods createVipCardFreeGoods();
}
5.6具体产品
5.6.1打折券的具体实现
public class PlatformDiscountFreeGoods extends DiscountFreeGoods {public boolean send(){System.out.println("发放平台打折券");}public List getSaleList(){// 这里返回平台售卖的商品}public BigDecimal calc(BigDecimal price){// 这里计算平台打折券使用后的优惠价格}
}
public class YoukuDiscountFreeGoods extends DiscountFreeGoods {public boolean send(){System.out.println("发放优酷打折券");}public List getSaleList(){// 这里调用优酷的接口返回优酷售卖的商品}public BigDecimal calc(BigDecimal price){// 这里调用用户的接口计算打折券使用后的优惠价格}
}
public class JDDiscountFreeGoods extends DisCountFreeGoods {public boolean send(){System.out.println("发放京东打折券");}public List getSaleList(){// 这里调用京东的接口返回京东售卖的商品}public BigDecimal calc(BigDecimal price){// 这里调用京东的接口计算打折券使用后的优惠价格}
}
5.6.2会员卡的具体实现
public class PlatformVipCardFreeGoods extends VipCardFreeGoods {public boolean send(){System.out.println("发放京东会员卡");}public boolean bindAccount(Long phoneNumber) {// 这里绑定平台账号}
}
public class YouKuVipCardFreeGoods extends VipCardFreeGoods {public boolean send(){System.out.println("发放京东会员卡");}public boolean bindAccount(Long phoneNumber) {// 这里调用优酷的接口,传手机号过去绑定用户账号}
}
public class JDVipCardFreeGoods extends VipCardFreeGoods {public boolean send(){System.out.println("发放京东会员卡");}public boolean bindAccount(Long phoneNumber) {// 这里调用京东的接口,传手机号过去绑定用户账号}
}
5.7具体工厂
public class PlatformFreeGoodsFactory implements FreeGoodsFactory {public DiscountFreeGoods createFreeGoods(){return new PlatformDiscountFreeGoods();}public VipCardFreeGoods createVipCardFreeGoods(){return new PlatformVipCardFreeGoods();}
}
public class YouKuFreeGoodsFactory implements FreeGoodsFactory {public DiscountFreeGoods createFreeGoods(){return new YoukuDiscountFreeGoods();}public VipCardFreeGoods createVipCardFreeGoods(){return new YouKuVipCardFreeGoods();}
}
public class JDFreeGoodsFactory implements FreeGoodsFactory {public DiscountFreeGoods createFreeGoods(){return new JDDiscountFreeGoods();}public VipCardFreeGoods createVipCardFreeGoods(){return new JDVipCardFreeGoods();}
}
5.8调用
public class Client {private DiscountFreeGoods discountFreeGoods;private VipCardFreeGoods vipCardFreeGoods;public Client(FreeGoodsFactory factory){this.discountFreeGoods = factory.createDiscountFreeGoods();this.vipCardFreeGoods = factory.createVipCardFreeGoods();}// 省略 get和set
}