外观模式
- 一、基本介绍
- 二、外观模式解决影院管理
-
- 三、注意事项
一、基本介绍
- 外观模式(Facade),也叫“过程模式”:外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
- 外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节。
二、外观模式解决影院管理
1. 案例要求
- 影院播放电影之前有各种各的准备工作,比如打开屏幕、打开声音、打开爆米花机器等等准备工作。
- 我们可以将每一种需求抽象为具体的类,类中具有具体的方法。如屏幕类中应该包括放下屏蔽和升起屏蔽的方法。
- 如果我们将所有的类都依次调用来完成准备工作显得过于繁琐。
2. 思路分析
- 可以创建一个外观类,让外观类去调用其他类中的所有方法。对不同类中的不同方法进行统一管理。
- 在外观类中创建我们需要的方法,如播放电影前的准备方法,播放电影过程中可能需要的暂停方法等等。
- 让客户端去调用外观类而不去调用具体类中的方法。
3. 代码实现
- DVDPlayer
public class DVDPlayer {
// 使用单例模式 饿汉式
private static DVDPlayer instance = new 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 is playing");
}
public void pause(){
System.out.println(" dvd pause");
}
}
- Popcorn
// 爆米花机器
public class Popcorn {
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance(){
return instance;
}
public void on(){
System.out.println(" popcorn on");
}
public void off(){
System.out.println(" popcorn off");
}
public void play(){
System.out.println(" popcorn is playing");
}
public void pause(){
System.out.println(" popcorn pause");
}
}
- Projector
// 声音
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance(){
return instance;
}
public void on(){
System.out.println(" projector on");
}
public void off(){
System.out.println(" projector off");
}
public void play(){
System.out.println(" projector is playing");
}
public void pause(){
System.out.println(" projector pause");
}
}
- Screen
// 屏幕
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance(){
return instance;
}
public void on(){
System.out.println(" screen up");
}
public void down(){
System.out.println(" screen down");
}
}
- Stereo
// 立体音响
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance(){
return instance;
}
public void on(){
System.out.println(" stereo up");
}
public void down(){
System.out.println(" stereo down");
}
public void up(){
System.out.println(" stereo up...");
}
}
- TheaterLight
// 灯光
public class TheaterLight {
private static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance(){
return instance;
}
public void on(){
System.out.println(" TheaterLight up");
}
public void down(){
System.out.println(" TheaterLight down");
}
public void dim(){
System.out.println(" TheaterLight dim...");
}
public void bright(){
System.out.println(" TheaterLight bright...");
}
}
- HomeTheaterFacade
// 外观类
public class HomeTheaterFacade {
// 定义各个子系统对象
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Screen screen;
private Stereo stereo;
private TheaterLight theaterLight;
// 构造器
public HomeTheaterFacade() {
this.dvdPlayer = DVDPlayer.getInstance();
this.popcorn = Popcorn.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.stereo = Stereo.getInstance();
this.theaterLight = TheaterLight.getInstance();
}
// 操作分四步
public void ready(){
popcorn.on(); // 打开爆米花机
popcorn.play(); // 打爆米花
screen.down(); // 放下屏幕
projector.on();//打开声音
stereo.on(); // 打开立体声
dvdPlayer.on(); // 打开dvd
theaterLight.dim(); // 调暗灯光
}
public void play(){
dvdPlayer.pause(); // 暂定播放
}
public void end(){
popcorn.off(); // 关闭爆米花机
theaterLight.bright(); // 打开灯
screen.on(); // 升起屏幕
projector.off(); // 关掉声音
stereo.up(); // 关掉立体声
dvdPlayer.off(); // 关掉dvd
}
}
- Client
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
homeTheaterFacade.play();
}
}
三、注意事项
- 外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性。
- 外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展。
- 通过合理的使用外观模式,可以帮助我们更好的划分访问层次。
- 当系统需要进行分层设计时,可以考虑使用Facade模式。