文章目录
1. 模式引出:影院管理项目
组建一个家庭影院:
DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的
功能,其过程为:
- 直接用遥控器:统筹各设备开关
- 开爆米花机
- 放下屏幕
- 开投影仪
- 开音响
- 开DVD,选dvd
- 去拿爆米花
- 调暗灯光
- 播放
- 观影结束后,关闭各种设备
1.1 传统方式解决影院管理
为每一个需要使用到的设备设计一个类,设计其中要使用的方法,然后让客户端调用( 每个类的方法名可能相同、 可能不同 )
UML举例
1.2 传统方式解决影院管理问题分析
-
在Main方法中,创建各个子系统的对象,并直接去调用子系统(对象) 相关方法,会造成调用过程混乱,没有清晰的过程
-
不利于在Main中,去维护对子系统的操作
-
解决思路:定义一个高层接口,给子系统中的一组接口提供一个一致的界面(比如在高层接口提供四个方法 ready, play, pause, end ),用来访问子系统中的一群接口
-
也就是说 就是通过定义一个一致的接口(界面类),用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节 => 外观模式
2. 外观模式基本介绍
-
外观模式(Facade),也叫“过程模式:外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
-
外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节
2.1 外观模式的原理类图
2.2 原理类图的说明(外观模式的角色)
-
外观类(Facade): 为调用端提供统一的调用接口, 外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当子系统对象
-
调用者(Client): 外观接口的调用者
-
子系统的集合:指模块或者子系统,处理Facade 对象指派的任务,他是功能的实际提供者
3. 方案修改
修改后的UML类图
为了简化获取实例的过程,具体设备类使用单例模式,因为不是必须,所以没有在UML中体现
DVDPlayer.java
public class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();
private DVDPlayer() {
}
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println("dvd on");
}
public void off() {
System.out.println("dvd off");
}
public void play() {
System.out.println("dvd play");
}
}
Popcorn.java
public class Popcorn {
private static Popcorn instance = new Popcorn();
private Popcorn() {
}
public static Popcorn getInstance() {
return instance;
}
public void on() {
System.out.println("Popcorn on");
}
public void pop() {
System.out.println("Popcorn poping");
}
public void off() {
System.out.println("Popcorn off");
}
}
Projector.java
public class Projector {
private static Projector instance = new Projector();
private Projector() {
}
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println("Projector on");
}
public void off() {
System.out.println("Projector off");
}
}
Screen.java
public class Screen {
private static Screen instance = new Screen();
private Screen() {
}
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("Screen up");
}
public void down() {
System.out.println("Screen down");
}
public void on() {
System.out.println("Screen on");
}
}
Stereo.java
public class Stereo {
private static Stereo instance = new Stereo();
private Stereo() {
}
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("Stereo on");
}
public void off() {
System.out.println("Stereo off");
}
}
TheatherLight.java
public class TheatherLight {
private static TheatherLight instance = new TheatherLight();
private TheatherLight() {
}
public static TheatherLight getInstance() {
return instance;
}
public void dim() {
System.out.println("TheatherLight dim");
}
public void bright() {
System.out.println("TheatherLight bright");
}
}
HomeTheaterFacade.java
public class HomeTheaterFacade {
private TheatherLight theatherLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dvdPlayer;
public HomeTheaterFacade() {
super();
this.theatherLight = TheatherLight.getInstance();
this.popcorn = Popcorn.getInstance();
this.stereo = Stereo.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dvdPlayer = DVDPlayer.getInstance();
}
public void ready() {
popcorn.on();
popcorn.pop();
screen.down();
projector.on();
screen.on();
dvdPlayer.on();
theatherLight.dim();
}
public void play() {
dvdPlayer.play();
}
public void pause() {
dvdPlayer.pause();
}
public void end() {
popcorn.off();
theatherLight.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
测试Main.java
public class Main {
public static void main(String[] args) {
HomeTheaterFacade theaterFacade = new HomeTheaterFacade();
theaterFacade.ready();
theaterFacade.play();
}
}
输出结果
4. 外观模式的注意事项和细节
-
外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
-
外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展
-
通过合理的使用外观模式,可以帮我们更好的划分访问的层次
-
当系统需要进行分层设计时,可以考虑使用Facade模式
-
在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性
-
不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利于维护为目的。