二十三种设计模式(第十种)-----外观模式(Facade)
尚硅谷视频连接https://www.bilibili.com/video/BV1G4411c7N4?from=search&seid=11487053970269878470
- 外观模式:又叫过程模式,外观模式为子系统中的一组接口提供一个一致的界面,此模式定义一个高层接口,这个接口使得这一子系统更容易使用
- 外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节
需求
组建一个家庭影院
DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要完全使用家庭影院的功能,具体过程为:
- 使用遥控器:统筹各设备开关
- 开爆米花机
- 放下屏幕
- 开投影仪
- 开音响
- 开DVD
- …
类图
代码
- 各子系统
//DVD播放
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");
}
}
------
//爆米花机
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 pop() {
System.out.println("popcorn is poping ");
}
}
-----
//投影仪
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 focus() {
System.out.println("Projector is focus ");
}
}
-----
//屏幕
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("Screen up");
}
public void down() {
System.out.println("Scren down");
}
}
-----
//立体声
public class Stereo {
public static Stereo instance = new Stereo();
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("Stereo on");
}
public void off() {
System.out.println("stereo off");
}
public void up() {
System.out.println("strero up..");
}
}
----
//剧场灯光
public class TheaterLight {
public static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance() {
return instance;
}
public void on() {
System.out.println("TheaterLight on");
}
public void off() {
System.out.println("TheaterLight off");
}
public void bright() {
System.out.println("TheaterLight dim..");
}
public void dim() {
System.out.println("TheaterLight dim..");
}
}
- 外观
public class HomeTheaterFacade {
//定义各个子系统的对象
private TheaterLight theaterLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dvdPlayer;
public HomeTheaterFacade() {
this.theaterLight = TheaterLight.getInstance();
this.popcorn = Popcorn.getInstance();
this.stereo = Stereo.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dvdPlayer = DVDPlayer.getInstance();
}
//操作分成4步
public void ready() {
popcorn.on();
screen.down();
projector.on();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}
public void play() {
dvdPlayer.play();
}
public void pause() {
dvdPlayer.pause();
}
public void end() {
theaterLight.bright();
popcorn.pop();
screen.down();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
- 客户端
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
homeTheaterFacade.end();
}
}