设计模式-外观模式

发布时间:2026/6/25 5:29:47

设计模式-外观模式 外观模式(Facade Pattern)详解一、模式概述外观模式是一种结构型设计模式,它为子系统中的一组接口提供一个统一的简化接口。外观模式定义了一个高层接口,让子系统更容易使用。就像酒店的前台——你不需要知道客房服务、餐饮服务、洗衣服务具体怎么运作,只需要告诉前台需求,前台会协调各个部门完成。二、模式结构1. 核心角色text┌─────────────────────────────────────────────────┐ │ Client │ │ (客户端) │ └─────────────────────┬───────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────┐ │ Facade │ │ (外观类) │ │ │ │ ┌──────────────┐ ┌──────────────┐ │ │ │ methodA() │ │ methodB() │ │ │ └──────┬───────┘ └──────┬───────┘ │ │ │ │ │ └───────────┼─────────────────┼────────────────────┘ │ │ ┌───────┴───────┐ ┌───────┴───────┐ │ │ │ │ ▼ ▼ ▼ ▼ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │SubSystemA│ │SubSystemB│ │SubSystemC│ │SubSystemD│ └─────────┘ └─────────┘ └─────────┘ └─────────┘2. 角色职责角色名称职责Facade外观类知道哪些子系统负责处理请求,将客户端请求委托给相应子系统SubSystem子系统类实现子系统的功能,处理由Facade指派的任务,对Facade一无所知Client客户端通过调用Facade来完成功能,无需直接与子系统交互三、基础实现示例示例:家庭影院系统java// 1. 子系统类:各个设备 class Amplifier { public void on() { System.out.println("功放打开"); } public void off() { System.out.println("功放关闭"); } public void setVolume(int level) { System.out.println("设置音量:" + level); } } class DVDPlayer { public void on() { System.out.println("DVD播放机打开"); } public void off() { System.out.println("DVD播放机关闭"); } public void play(String movie) { System.out.println("播放电影:" + movie); } } class Projector { public void on() { System.out.println("投影仪打开"); } public void off() { System.out.println("投影仪关闭"); } public void setInput(String input) { System.out.println("设置输入源:" + input); } } class Screen { public void down() { System.out.println("幕布下降"); } public void up() { System.out.println("幕布上升"); } } class Lights { public void dim(int level) { System.out.println("灯光调暗至:" + level + "%"); } public void bright() { System.out.println("灯光调亮"); } } // 2. 外观类:家庭影院外观 class HomeTheaterFacade { private Amplifier amp; private DVDPlayer dvd; private Projector projector; private Screen screen; private Lights lights; public HomeTheaterFacade(Amplifier amp, DVDPlayer dvd, Projector projector, Screen screen, Lights lights) { this.amp = amp; this.dvd = dvd; this.projector = projector; this.screen = screen; this.lights = lights; } // 简化的观影方法 public void watchMovie(String movie) { System.out.println("===== 准备观看电影 ====="); lights.dim(10); // 灯光调暗 screen.down(); // 幕布下降 projector.on(); // 投影仪打开 projector.setInput("DVD"); amp.on(); // 功放打开 amp.setVolume(20); // 设置音量 dvd.on(); // DVD打开 dvd.play(movie); // 播放电影 } // 简化的结束观影方法 public void endMovie() { System.out.println("===== 结束观看电影 ====="); dvd.off(); amp.off(); projector.off(); screen.up(); lights.bright(); } } // 3. 客户端:使用外观模式 public class HomeTheaterTest { public static void main(String[] args) { // 创建子系统组件 Amplifier amp = new Amplifier(); DVDPlayer dvd = new DVDPlayer(); Projector projector = new Projector(); Screen screen = new Screen(); Lights lights = new Lights(); // 创建外观 HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, dvd, projector, screen, lights); // 使用简化的接口 homeTheater.watchMovie("盗梦空间"); /

相关新闻