当前位置: 首页 > news >正文

设计模式之外观模式

目录

  • 定义
  • 结构
  • 适用场景
  • 使用示例

定义

       外观模式(Facade Pattern)‌ 是一种结构型设计模式,它提供了一个统一的接口,用来访问子系统中的一组接口。外观模式定义了一个高层接口,这个接口使得子系统更容易使用。

结构

在这里插入图片描述

适用场景

       1)为复杂的子系统提供简单入口
       2)统一管理系统中存在的多个复杂的子系统
       3)解耦客户端与多个子系统之间的依赖关系
       4)分层系统中,作为层与层之间的通信接口
       5)为遗留系统提供新的简化接口

使用示例

      简单的电商订单示例
      定义各个模拟子系统

/*** 库存子系统*/
public class InventoryService {public boolean checkStock(String productId, int quantity) {System.out.println("检查库存: 产品" + productId + " 数量" + quantity);return true;}public void lockStock(String productId, int quantity) {System.out.println("锁定库存: 产品" + productId + " 数量" + quantity);}}
/*** 通知子系统*/
public class NotificationService {public void sendConfirmation(String userId, String orderId) {System.out.println("发送确认通知: 用户" + userId + " 订单" + orderId);}}
/*** 支付子系统*/
public class PaymentService {public boolean processPayment(String userId, double amount) {System.out.println("处理支付: 用户" + userId + " 金额" + amount);return true;}}
/*** 物流子系统*/
public class ShippingService {public String scheduleDelivery(String orderId) {String trackingNo = "TRK" + System.currentTimeMillis();System.out.println("安排配送: 订单" + orderId + " 物流单号" + trackingNo);return trackingNo;}}

      定义外观门面

/*** 外观:订单处理门面*/
public class OrderProcessFacade {private InventoryService inventory;private PaymentService payment;private ShippingService shipping;private NotificationService notification;public OrderProcessFacade() {this.inventory = new InventoryService();this.payment = new PaymentService();this.shipping = new ShippingService();this.notification = new NotificationService();}// 统一订单处理接口public String placeOrder(String userId, String productId, int quantity, double amount) {System.out.println("\n开始处理订单...");if (!inventory.checkStock(productId, quantity)) {throw new RuntimeException("库存不足");}inventory.lockStock(productId, quantity);if (!payment.processPayment(userId, amount)) {throw new RuntimeException("支付失败");}String orderId = "ORD" + System.currentTimeMillis();String trackingNo = shipping.scheduleDelivery(orderId);notification.sendConfirmation(userId, orderId);System.out.println("订单处理完成: " + orderId);return orderId;}}

      测试

public class OrderController {public static void main(String[] args) {OrderProcessFacade orderFacade = new OrderProcessFacade();String orderId = orderFacade.placeOrder("user123", "prod1001", 2, 299.99);System.out.println("生成的订单号: " + orderId);}}
http://www.lqws.cn/news/577297.html

相关文章:

  • 【微服务】.Net中使用Consul实现服务高可用
  • 大语言模型微调的效能控制与评估策略
  • 提示技术系列——链式提示
  • 跨主机用 Docker Compose 部署 PostgreSQL + PostGIS 主从
  • 对象池模式:减少GC的Kotlin实战指南
  • 基于 SpringBoot+Vue.js+ElementUI 的 Cosplay 论坛设计与实现7000字论文
  • LeetCode 1456. 定长子串中元音的最大数目
  • MapReduce
  • EtherCAT主站教程4--IGH主站代码详解
  • 云手机的用途都有哪些?
  • Deep Mean-Shift Priors for Image Restoration论文阅读
  • mysql mvcc
  • Hadoop WordCount 程序实现与执行指南
  • Java 案例 6 - 数组篇(基础)
  • 第 89 场周赛:山脉数组的峰值索引、车队、考场就坐、相似度为 K 的字符串
  • 大语言模型(LLM)笔记
  • UE5 一台电脑+双显示器 配置nDisplay裸眼3D效果
  • 东芝TC78S600FNG在打印机中的应用:静音、防卡纸与能效
  • Python 数据分析与机器学习入门 (八):用 Scikit-Learn 跑通第一个机器学习模型
  • 智慧畜牧-猪场猪只行为状态检测数据集VOC+YOLO格式3790张15类别
  • Java中for与foreach
  • python+uniapp基于微信小程序的生鲜订购系统nodejs+java
  • 基于uniapp的老年皮肤健康管理微信小程序平台(源码+论文+部署+安装+售后)
  • JAVA八股文:异常有哪些种类,可以举几个例子吗?Throwable类有哪些常见方法?
  • HTML5 实现的圣诞主题网站源码,使用了 HTML5 和 CSS3 技术,界面美观、节日氛围浓厚。
  • 湖北理元理律师事务所债务解法:从法律技术到生活重建
  • 车载Tier1 supplier梳理
  • VMware vSphere 9与ESXi 9正式发布:云原生与AI驱动的虚拟化平台革新
  • Nginx反向代理与缓存功能
  • 【软考高项论文】信息系统项目的资源管理