当前位置: 首页 > 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.dtcms.com/a/262757.html

相关文章:

  • Hadoop WordCount 程序实现与执行指南
  • MidJourney生成东汉末年项羽全身像提示词
  • 多线程环境下的线程安全资源与缓存池设计:ThreadSafeObject 与 CachePool 实例解析
  • 深入理解 MVCC:数据库高并发的核心引擎
  • LabVIEW键盘鼠标监测控制
  • 七天学会SpringCloud分布式微服务——06——Sentinel
  • 【软考中级·软件评测师】下午题·面向对象测试之架构考点全析:分层、分布式、微内核与事件驱动
  • 通过python+openCV实现对图片中箭头方向的判断
  • LeetCode 594. 最长和谐子序列
  • 关于 java:8. Java 内存模型与 JVM 基础
  • 汇编基础介绍——ARMv8指令集(四)
  • 【c/c++1】数据类型/指针/结构体,static/extern/makefile/文件
  • 【c/c++3】类和对象,vector容器,类继承和多态,systemd,stdboost
  • Ragflow本地部署和基于知识库的智能问答测试
  • 机器学习在智能电网中的应用:负荷预测与能源管理
  • 【鸿蒙中级】
  • 面试复盘6.0
  • 「Java案例」输出24个希腊字母
  • 深入理解 Dubbo 负载均衡:原理、源码与实践
  • Redis Cluster Gossip 协议
  • 指针篇(6)- sizeof和strlen,数组和指针笔试题
  • 免费SSL证书一键申请与自动续期
  • MySQL-复合查询
  • 暴力风扇方案介绍
  • AlpineLinux安装部署MariaDB
  • 微信小程序接入腾讯云短信验证码流程
  • 用户行为序列建模(篇十)-【加州大学圣地亚哥分校】SASRec
  • 在Linux系统中部署Java项目
  • Unity Catalog 三大升级:Data+AI 时代的统一治理再进化
  • Re:从0开始的 空闲磁盘块管理(考研向)