标签:接口 失败 发放 throw 延迟 code 体验 vat 系统
工厂模式,是一种创建型设计模式,其在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。
工厂模式是 Java 开发中最常见的一种模式,其主要意图是定义一个创建对象的接口,让其子类决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。
简单说就是为了提供代码的可扩展性,屏蔽每一个功能类中的具体实现逻辑。让外部可以更加简单的调用。同时,可以去掉众多 ifelse
。
优点: 1、一个调用者想创建一个对象,只要知道其名称就可以了。 2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 3、屏蔽产品的具体实现,调用者只关心产品的接口。
缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。
营销场景中经常会有抽奖活动,我们在这里模拟兑换多种类型的商品。
假如我们有如下三种类型的商品接口:
序号 | 类型 | 接口 |
---|---|---|
1 | 代金劵 | VoucherResult sendVoucher(String userId, String voucherId, int voucherNumber) |
2 | VIP体验卡 | void openVIP(String userId, Date date, String time) |
3 | 实物奖品 | Boolean deliverGoods(DeliverRequest req) |
public class PrizeController {
private static final String CODE_SUCCESS = "11111111";
private static final String CODE_FAIL = "00000000";
public AwardResult awardToUser(AwardRequest req) {
AwardResult res = null;
// 按照不同类型分法商品[1代金劵、2VIP体验卡、3实物奖品]
if (req.getAwardType() == 1) {
VoucherService voucherService = new VoucherService();
VoucherResult voucherResult = voucherService.sendVoucher(req.getUserId(), req.getAwardId(), req.getAwardNumber());
if (CODE_SUCCESS.equals(voucherResult.getCode())) {
res = new AwardResult(CODE_SUCCESS, "成功");
} else {
res = new AwardResult(CODE_FAIL, voucherResult.getInfo());
}
} else if (req.getAwardType() == 2) {
VIPService vipService = new VIPService();
Date date = new Date(System.currentTimeMillis());
vipService.openVip(req.getUserId(), date, req.getTime());
res = new AwardResult(CODE_SUCCESS, "成功");
} else if (req.getAwardType() == 3) {
GoodsService goodsService = new GoodsService();
DeliverRequest deliverRequest = new DeliverRequest();
deliverRequest.setUserId(req.getUserId());
deliverRequest.setUserName(queryUserNameById(req.getUserId()));
deliverRequest.setUserPhone(queryUserPhoneById(req.getUserId()));
deliverRequest.setUserAddress(queryUserAddressById(req.getUserId()));
Boolean isSuccess = goodsService.deliverGoods(deliverRequest);
if (isSuccess) {
res = new AwardResult(CODE_SUCCESS, "成功");
} else {
res = new AwardResult(CODE_FAIL, "失败");
}
}
return res;
}
public String queryUserNameById(String userId) {
return "Tom";
}
public String queryUserPhoneById(String userId) {
return "123456789";
}
public String queryUserAddressById(String userId) {
return "BeiJin";
}
}
ifelse
实现业务需求。如果仅从业务角度看,已经实现了基本功能。@Test
public void testAwardToUser() {
PrizeController prizeController = new PrizeController();
AwardResult awardResult = null;
System.out.println("******模拟多种奖品发放测试******");
AwardRequest awardReq = new AwardRequest();
awardReq.setAwardId("0001");
awardReq.setAwardNumber(1);
awardReq.setUserId("10001");
System.out.println("\n------ 代金券的发放 ------");
awardReq.setAwardType(1);
awardResult = prizeController.awardToUser(awardReq);
System.out.println("代金券发放结果:" + awardResult.getRes());
System.out.println("\n------ VIP体验卡的发放 -------");
awardReq.setAwardType(2);
awardResult = prizeController.awardToUser(awardReq);
System.out.println("VIP体验卡发放结果:" + awardResult.getRes());
System.out.println("\n------ 实物奖品的发放 -------");
awardReq.setAwardType(3);
awardResult = prizeController.awardToUser(awardReq);
System.out.println("实物奖品发放结果:" + awardResult.getRes());
}
结果:
public interface IAward {
void sendAward(String userId, String awardId, Map<String, Object> map);
}
代金券
public class VoucherAwardService implements IAward{
private VoucherService voucherService = new VoucherService();
@Override
public void sendAward(String userId, String awardId, Map<String, Object> map) throws Exception{
VoucherResult voucherResult = voucherService.sendVoucher(userId, awardId, (int) map.get("voucherNumber"));
if (PrizeController.CODE_FAIL.equals(voucherResult.getCode())) {
throw new RuntimeException("失败");
}
}
}
VIP体验卡
public class VIPAwardService implements IAward{
private VIPService vipService = new VIPService();
@Override
public void sendAward(String userId, String awardId, Map<String, Object> map) throws Exception {
Date date = new Date(System.currentTimeMillis());
vipService.openVip(userId, (Date) map.get("data"), (String) map.get("time"));
}
}
实物奖品
public class GoodsAwardService implements IAward{
private GoodsService goodsService = new GoodsService();
@Override
public void sendAward(String userId, String awardId, Map<String, Object> map) throws Exception {
DeliverRequest deliverRequest = new DeliverRequest();
deliverRequest.setUserId(userId);
deliverRequest.setUserName(queryUserNameById(userId));
deliverRequest.setUserPhone(queryUserPhoneById(userId));
deliverRequest.setUserAddress(queryUserAddressById(userId));
Boolean isSuccess = goodsService.deliverGoods(deliverRequest);
if (!isSuccess) {
throw new RuntimeException("失败");
}
}
public String queryUserNameById(String userId) {
return "Tom";
}
public String queryUserPhoneById(String userId) {
return "123456789";
}
public String queryUserAddressById(String userId) {
return "BeiJin";
}
}
public class AwardFactory {
public IAward getAwardService(Integer awardType) throws Exception{
if (awardType == null) return null;
if (1 == awardType) return new VoucherAwardService();
if (2 == awardType) return new VIPAwardService();
if (3 == awardType) return new GoodsAwardService();
throw new RuntimeException("不存在此奖品类型");
}
}
@Test
public void testIAward() throws Exception{
AwardFactory awardFactory = new AwardFactory();
System.out.println("******模拟多种奖品发放测试******");
System.out.println("\n------ 代金券的发放 ------");
IAward awardService1 = awardFactory.getAwardService(1);
Map<String, Object> map1 = new HashMap<>();
map1.put("voucherNumber", 1);
awardService1.sendAward("10001", "123456712", map1);
System.out.println("\n------ VIP体验卡的发放 -------");
IAward awardService2 = awardFactory.getAwardService(2);
Map<String, Object> map2 = new HashMap<>();
map2.put("data", new Date(System.currentTimeMillis()));
map2.put("time", "23:59:59");
awardService2.sendAward("10002", "123123414", map2);
System.out.println("\n------ 实物奖品的发放 -------");
IAward awardService3 = awardFactory.getAwardService(3);
awardService3.sendAward("10003", "12312451", null);
}
工厂模式可以避免创建者与具体的产品逻辑耦合、满足单一职责,每一个业务逻辑实现都在所属自己的类中完成、满足开闭原则,无需修改调用方就可以在程序中引入新的产品类型。
但这样也会带来一些问题,比如有非常多的奖品类型,那么实现的子类会急速扩张。
标签:接口 失败 发放 throw 延迟 code 体验 vat 系统
原文地址:https://www.cnblogs.com/Code-CHAN/p/14441804.html