设计模式之外观模式

目录

定义

外观模式(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);
    }

}
相关推荐
ShineWinsu13 分钟前
对于单链表相关经典算法题:206. 反转链表及876. 链表的中间结点的解析
java·c语言·数据结构·学习·算法·链表·力扣
迦蓝叶18 分钟前
JAiRouter 配置文件重构纪实 ——基于单一职责原则的模块化拆分与内聚性提升
java·网关·ai·重构·openai·prometheus·单一职责原则
ST.J20 分钟前
系统架构思考20241204
java·笔记·系统架构
TDengine (老段)39 分钟前
TDengine 时间函数 TIMETRUNCATE 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
摘星编程1 小时前
CodeBuddy 辅助重构:去掉 800 行 if-else 的状态机改造
设计模式·代码重构·技术债务·codebuddy·状态机模式
Meteors.1 小时前
23种设计模式——策略模式 (Strategy Pattern)详解
设计模式·策略模式
给我个面子中不1 小时前
JUC、JVM八股补充
java·开发语言·jvm
mask哥2 小时前
详解flink性能优化
java·大数据·微服务·性能优化·flink·kafka·stream
hqxstudying2 小时前
Kafka 深入研究:从架构革新到性能优化的全面解析
java·开发语言·微服务·kafka·springcloud
失散133 小时前
并发编程——17 CPU缓存架构详解&高性能内存队列Disruptor实战
java·缓存·架构·并发编程