定義一個高層接口位迂,為子系統(tǒng)中的一組接口提供一個一致的外觀访雪,從而簡化了該子系統(tǒng)的使用。
package com.strife.pattern.facade;
/**
* 外觀模式
*
* @author mengzhenghao
* @date 2022/5/30
*/
public class Facade {
public static void main(String[] args) {
HomeFacade homeFacade = new HomeFacade();
homeFacade.watchMovie();
System.out.println("----------------------------------");
homeFacade.endMovie();
}
}
class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println("DVDPlayer on");
}
public void off() {
System.out.println("DVDPlayer off");
}
public void play() {
System.out.println("DVDPlayer is playing");
}
public void pause() {
System.out.println("DVDPlayer is paused");
}
}
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 popping");
}
}
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 tvMode() {
System.out.println("Projector is in tv mode");
}
public void wideScreenMode() {
System.out.println("Projector is in wide screen mode");
}
}
class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("Screen is going up");
}
public void down() {
System.out.println("Screen is going down");
}
}
class HomeFacade {
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Screen screen;
public HomeFacade() {
this.dvdPlayer = DVDPlayer.getInstance();
this.popcorn = Popcorn.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
}
public void watchMovie() {
popcorn.on();
popcorn.pop();
dvdPlayer.on();
dvdPlayer.play();
screen.down();
projector.on();
projector.tvMode();
}
public void endMovie() {
popcorn.off();
dvdPlayer.off();
screen.up();
projector.off();
}
}