当前位置: 首页 > 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.xdnf.cn/news/1075141.html

相关文章:

  • .net8导出影像图片按现场及天拆分
  • 调试W5500(作为服务器)
  • macos 使用 vllm 启动模型
  • 【微服务】.Net中使用Consul实现服务高可用
  • 51c大模型~合集144
  • 2025年光学工程、精密仪器与光电子技术国际会议(OEPIOT 2025)
  • 物联网基础
  • Git 常用命令、常用错误的总结
  • 2 大语言模型基础-2.2 生成式预训练语言模型GPT-2.2.2 有监督下游任务微调-Instruct-GPT强化学习奖励模型的结构改造与维度转换解析
  • [论文阅读] Neural Architecture Search: Insights from 1000 Papers
  • 超表面重构卡塞格林望远镜 | 从传统架构到新型光学系统
  • 最大矩形最大正方形-力扣
  • 优雅草蜻蜓HR人才招聘系统v2.0.9上线概要 -优雅草新产品上线
  • 飞算JavaAI 2.0.0深度测评:自然语言编程如何重构开发生产力?
  • 键盘第一下无反应
  • 04密码加密
  • C#程序调用cmd执行命令
  • 卡片跳转到应用页面(router事件)
  • 生成式人工智能实战 | 变分自编码器(Variational Auto-Encoder, VAE)
  • 基于STM32温湿度检测—串口显示
  • HTML5 实现的圣诞主题网站源码,使用了 HTML5 和 CSS3 技术,界面美观、节日氛围浓厚。
  • k8s pod深度解析
  • k8s创建定时的 Python 任务(CronJob)
  • 【c/c++1】数据类型/指针/结构体,static/extern/makefile/文件
  • 机器学习9——决策树
  • 新生代潜力股刘小北:演艺路上的璀璨新星
  • ROS常用的路径规划算法介绍
  • 面试复盘6.0
  • Java面试宝典:基础四
  • SpringSecurity6-oauth2-三方gitee授权-授权码模式