外观模式,改造前
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.facade;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 外观类——礼物兑换逻辑
|
||||||
|
* <p>
|
||||||
|
* 该服务定义了与各个子系统交互的逻辑,业务系统无需知道与内部子系统的交互逻辑,符合迪米特法则
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/28 10:44
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class GiftExchangeService {
|
||||||
|
|
||||||
|
private PointPaymentService pointPaymentService;
|
||||||
|
|
||||||
|
private QualifyService qualifyService;
|
||||||
|
|
||||||
|
private ShippingService shippingService;
|
||||||
|
|
||||||
|
public void setPointPaymentService(PointPaymentService pointPaymentService) {
|
||||||
|
this.pointPaymentService = pointPaymentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQualifyService(QualifyService qualifyService) {
|
||||||
|
this.qualifyService = qualifyService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShippingService(ShippingService shippingService) {
|
||||||
|
this.shippingService = shippingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统交互逻辑
|
||||||
|
*
|
||||||
|
* @param pointGift
|
||||||
|
*/
|
||||||
|
public void giftExchange(PointGift pointGift) {
|
||||||
|
if (qualifyService.isAvalible(pointGift)) {
|
||||||
|
// 资格校验通过
|
||||||
|
if (pointPaymentService.pay(pointGift)) {
|
||||||
|
// 如果积分支付成功
|
||||||
|
String shippingOrderNo = shippingService.shipGift(pointGift);
|
||||||
|
log.info("物流系统下单成功,订单号为{}", shippingOrderNo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.facade;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 外观模式又叫门面模式,提供了一个统一的接口,用来访问子系统中的一群接口
|
||||||
|
* 积分礼物
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/28 10:33
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PointGift {
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.facade;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 积分支付校验子系统
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/28 10:37
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class PointPaymentService {
|
||||||
|
|
||||||
|
public boolean pay(PointGift pointGift) {
|
||||||
|
// 扣减积分逻辑
|
||||||
|
log.info("支付礼物 {} 积分成功", pointGift.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.facade;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 积分校验子系统
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/28 10:34
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class QualifyService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模拟积分校验逻辑
|
||||||
|
*
|
||||||
|
* @param pointGift
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean isAvalible(PointGift pointGift) {
|
||||||
|
log.info("校验礼物 {} 通过,积分校验通过,库存通过", pointGift.getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.facade;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 物流子系统
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/28 10:39
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ShippingService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回物流运单号
|
||||||
|
*
|
||||||
|
* @param pointGift
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String shipGift(PointGift pointGift) {
|
||||||
|
// 物流系统对接逻辑
|
||||||
|
log.info("礼物 {} 进入物流系统", pointGift.getName());
|
||||||
|
return "123456";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package top.fjy8018.designpattern.pattern.structural.decorator.before;
|
package top.fjy8018.designpattern.pattern.structural.decorator;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -7,8 +7,6 @@ import top.fjy8018.designpattern.pattern.structural.decorator.after.AbstractBatt
|
|||||||
import top.fjy8018.designpattern.pattern.structural.decorator.after.EggBattercake;
|
import top.fjy8018.designpattern.pattern.structural.decorator.after.EggBattercake;
|
||||||
import top.fjy8018.designpattern.pattern.structural.decorator.after.SausageBattercake;
|
import top.fjy8018.designpattern.pattern.structural.decorator.after.SausageBattercake;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
class BattercakeTest {
|
class BattercakeTest {
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.structural.facade;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/2/28 10:53
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
class GiftExchangeServiceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void giftExchange() {
|
||||||
|
PointGift pointGift = new PointGift("手环");
|
||||||
|
// 注入服务
|
||||||
|
GiftExchangeService exchangeService = new GiftExchangeService();
|
||||||
|
exchangeService.setPointPaymentService(new PointPaymentService());
|
||||||
|
exchangeService.setQualifyService(new QualifyService());
|
||||||
|
exchangeService.setShippingService(new ShippingService());
|
||||||
|
// 兑换
|
||||||
|
exchangeService.giftExchange(pointGift);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user