1 代码演练
1.1 代码演练1(不使用外观模式)
1.2 代码演练2(使用外观模式)
2 应用核心
2.1 核心
2.2 零碎知识点
1 代码演练
1.1 代码演练1(不使用外观模式)
需求:
木木网有积分兑换礼物的功能,设计的时候需要校验三步:
a 资格校验系统,是木木网会员。
b 积分系统,该系统放的是各个积分的获取支出,需要拿出该用户目前的积分和该礼物所需要的积分进行对比
c 物流系统,如果满足ab,则返回成功,并返回一个订单号。
关注点:
应用层无需知道资格校验类等其他子系统的类
UML类图:
测试类:
package com.geely.design.pattern.structural.facade; /** * 测试外观模式 */ public class Test { public static void main(String [] args){ GiftExchangeService giftExchangeService = new GiftExchangeService(); PointGift pointGift = new PointGift("熊猫抱枕"); /** * 下边要注入三个子系统,我私下认为这种方式很不常规,测试类我只需要知道交互的类就可以了 * 也就是说,我只需要知道giftExchange即可 */ giftExchangeService.setQualifyService(new QualifyService()); giftExchangeService.setPointPaymentService(new PointPaymentService()); giftExchangeService.setShippingService(new ShippingService()); giftExchangeService.giftExchange(pointGift); } }
外观类:
package com.geely.design.pattern.structural.facade; /** * 外观类 */ public class GiftExchangeService { private QualifyService qualifyService; private PointPaymentService pointPaymentService; private ShippingService shippingService; /** * 初始化的时候把这些服务类进行注入 * @param qualifyService */ public void setQualifyService(QualifyService qualifyService) { this.qualifyService = qualifyService; } public void setPointPaymentService(PointPaymentService pointPaymentService) { this.pointPaymentService = pointPaymentService; } public void setShippingService(ShippingService shippingService) { this.shippingService = shippingService; } public void giftExchange(PointGift pointGift){ if(qualifyService.getQualify(pointGift)){ //如果资格通过,校验积分是否通过,积分通过,会进行发货交易 if(pointPaymentService.pay(pointGift)){ shippingService.shipGift(pointGift); } } } }
资格验证系统类:
package com.geely.design.pattern.structural.facade; /** * 资格校验类 */ public class QualifyService { /** * 获取是否得到校验资格 * @param pointGift * @return */ public boolean getQualify(PointGift pointGift){ //判断是否木木网会员类逻辑 System.out.println(pointGift.getGiftName()+"资格校验通过!"); return true; } }
积分系统类:
package com.geely.design.pattern.structural.facade; /** * 积分校验类,校验积分是否足够 */ public class PointPaymentService { public boolean pay(PointGift pointGift){ System.out.println(pointGift.getGiftName()+"积分校验通过!"); return true; } }
物流系统类:
package com.geely.design.pattern.structural.facade; /** * 物流子系统 */ public class ShippingService { public String shipGift(PointGift pointGift){ //其他校验逻辑 String orderNo = "999"; System.out.println(pointGift.getGiftName()+"发送物流成功,订单号为"+orderNo); return orderNo; } }
实体类:
package com.geely.design.pattern.structural.facade; /** * 礼物积分类,作为基类存在,其他服务类调取本类 */ public class PointGift { /** * 通过构造器声明积分礼物,方便Test类进行测试 * @param giftName */ public PointGift(String giftName) { this.giftName = giftName; } private String giftName;//礼物名称 public String getGiftName() { return giftName; } public void setGiftName(String giftName) { this.giftName = giftName; } }
打印结果:
"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=19215:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes" com.geely.design.pattern.structural.facade.Test 熊猫抱枕资格校验通过! 熊猫抱枕积分校验通过! 熊猫抱枕发送物流成功,订单号为999 Process finished with exit code 0
1.2 代码演练2(使用外观模式)
测试类:
package com.geely.design.pattern.structural.facade; /** * 测试外观模式 */ public class Test { public static void main(String [] args){ GiftExchangeService giftExchangeService = new GiftExchangeService(); PointGift pointGift = new PointGift("熊猫抱枕"); /** * 下边要注入三个子系统,我私下认为这种方式很不常规,测试类我只需要知道交互的类就可以了 * 也就是说,我只需要知道giftExchange即可 */ /* giftExchangeService.setQualifyService(new QualifyService()); giftExchangeService.setPointPaymentService(new PointPaymentService()); giftExchangeService.setShippingService(new ShippingService());*/ giftExchangeService.giftExchange(pointGift); } }
外观类:
package com.geely.design.pattern.structural.facade; /** * 外观类 */ public class GiftExchangeService { private QualifyService qualifyService = new QualifyService(); private PointPaymentService pointPaymentService = new PointPaymentService(); private ShippingService shippingService = new ShippingService(); /** * 初始化的时候把这些服务类进行注入 * @param qualifyService */ /*public void setQualifyService(QualifyService qualifyService) { this.qualifyService = qualifyService; } public void setPointPaymentService(PointPaymentService pointPaymentService) { this.pointPaymentService = pointPaymentService; } public void setShippingService(ShippingService shippingService) { this.shippingService = shippingService; }*/ public void giftExchange(PointGift pointGift){ if(qualifyService.getQualify(pointGift)){ //如果资格通过,校验积分是否通过,积分通过,会进行发货交易 if(pointPaymentService.pay(pointGift)){ shippingService.shipGift(pointGift); } } } }
2 应用核心
2.1 核心
应用层不关心子系统,应用层只和外观类通信,子系统只和外观类通信。
2.2 零碎知识点
因为没有集成Spring框架,所以使用set方法注入
1.2案例假设初始化的时候服务的类都是创建好的。
如果继续增加子系统的话,使用实体外观类的话,不符合开闭原则,如果使用抽象外观类或者外观接口的话,符合开闭原则。
诸葛