全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-11 5 分钟 ✍️ juanwangdev

Java外观模式

外观模式为复杂子系统提供简化接口,隐藏内部复杂性。

模式定义

意图:为子系统提供统一入口,简化客户端调用。

适用场景

  • 复杂子系统需要简化接口
  • 分层架构中的入口层
  • 解耦客户端与子系统

模式结构

子系统类

Java
public class SystemA {
    public void operationA() {
        System.out.println("系统A操作");
    }
}

public class SystemB {
    public void operationB() {
        System.out.println("系统B操作");
    }
}

public class SystemC {
    public void operationC() {
        System.out.println("系统C操作");
    }
}

外观类

Java
public class Facade {
    private SystemA systemA = new SystemA();
    private SystemB systemB = new SystemB();
    private SystemC systemC = new SystemC();

    // 简化接口:一键操作
    public void simpleOperation() {
        systemA.operationA();
        systemB.operationB();
        systemC.operationC();
    }

    // 组合接口:特定功能
    public void specialOperation() {
        systemA.operationA();
        systemC.operationC();
    }
}

使用示例

Java
// 客户端只需调用外观类
Facade facade = new Facade();
facade.simpleOperation();  // 自动调用A、B、C

// 不需要了解子系统细节

实际应用示例

注册流程

Java
// 子系统
public class UserService {
    public void register(String username) {
        System.out.println("注册用户: " + username);
    }
}

public class EmailService {
    public void sendEmail(String email) {
        System.out.println("发送验证邮件: " + email);
    }
}

public class SmsService {
    public void sendSms(String phone) {
        System.out.println("发送验证短信: " + phone);
    }
}

public class LogService {
    public void log(String message) {
        System.out.println("记录日志: " + message);
    }
}

// 外观类
public class RegisterFacade {
    private UserService userService = new UserService();
    private EmailService emailService = new EmailService();
    private SmsService smsService = new SmsService();
    private LogService logService = new LogService();

    public void register(String username, String email, String phone) {
        userService.register(username);
        emailService.sendEmail(email);
        smsService.sendSms(phone);
        logService.log("注册完成: " + username);
    }
}

// 使用
RegisterFacade facade = new RegisterFacade();
facade.register("张三", "zhang@example.com", "13800000000");

JDBC外观

JDBC连接过程复杂,可封装为外观:

Java
public class JdbcFacade {
    private Connection connection;

    public void connect(String url, String user, String password) {
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            throw new RuntimeException("连接失败", e);
        }
    }

    public List<Map<String, Object>> query(String sql) {
        try {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            List<Map<String, Object>> result = new ArrayList<>();
            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();
                ResultSetMetaData meta = rs.getMetaData();
                for (int i = 1; i <= meta.getColumnCount(); i++) {
                    row.put(meta.getColumnName(i), rs.getObject(i));
                }
                result.add(row);
            }
            return result;
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        }
    }

    public void close() {
        try {
            if (connection != null) connection.close();
        } catch (SQLException e) {}
    }
}

// 使用
JdbcFacade jdbc = new JdbcFacade();
jdbc.connect(url, user, pass);
List<Map<String, Object>> data = jdbc.query("SELECT * FROM users");
jdbc.close();

外观模式优点

  1. 简化复杂子系统调用
  2. 解耦客户端与子系统
  3. 分层架构入口
  4. 不破坏子系统独立性

外观 vs 代理

特性外观代理
目的简化接口控制访问
对象多个子系统单个对象
新功能组合已有功能可添加额外逻辑

适用场景

  1. 复杂系统需要简单入口
  2. 分层架构的入口层(如Service层)
  3. 遗留系统重构

注意事项

外观不添加新业务逻辑,只组合调用

子系统仍可直接使用,外观只是可选入口

一个系统可有多个外观类

外观模式是最简单、最常用的模式

要点总结

  1. 外观模式为复杂子系统提供简化接口
  2. 客户端只与外观类交互,不接触子系统
  3. 外观组合子系统方法,不添加新逻辑
  4. JDBC连接封装是典型应用
  5. 分层架构中Service层就是外观模式的体现

📝 发现内容有误?点击此处直接编辑

← 上一篇 Java备忘录模式
下一篇 → Java工厂模式
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库