代码
package com.hao.demo.design.facade;
/**
* @author haoxiansheng
* @date 2020-05-18
*/
public class DVDPlayer {
// 使用单例模式
private static DVDPlayer instance = new DVDPlayer();
// 饿汉式
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println("DVD 打开了");
}
public void off() {
System.out.println("DVD 关闭了");
}
public void play() {
System.out.println("DVD 开始play");
}
public void pause() {
System.out.println("DVD pause");
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
public class Popcorn {
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance() {
return instance;
}
public void on() {
System.out.println("Popcorn 打开了");
}
public void off() {
System.out.println("Popcorn 关闭了");
}
public void poping() {
System.out.println("Popcorn 开始制作爆米花");
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println("Projector 打开了");
}
public void off() {
System.out.println("Projector 关闭了");
}
public void focus() {
System.out.println("Projector is focusing");
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
public void screeUp() {
System.out.println("scree Up");
}
public void screeDowm() {
System.out.println("scree Dowm ");
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("Stereo 打开了");
}
public void off() {
System.out.println("Stereo 关闭了");
}
public void up() {
System.out.println("Stereo 声音调大");
}
public void down() {
System.out.println("Stereo 声音减小");
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
public class TheaterLight {
private 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 dim() {
System.out.println("TheaterLight dim --");
}
public void bright() {
System.out.println("TheaterLight bright");
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
// 外观类
public class HomeTheaterFacade {
// 定义属性
private TheaterLight theaterLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dvdPlayer;
// 构造器
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.poping();
screen.screeDowm();
projector.on();
stereo.on();
dvdPlayer.on();
dvdPlayer.on();
theaterLight.dim();
}
public void play() {
dvdPlayer.play();
stereo.up();
stereo.down();
}
public void pause() {
dvdPlayer.pause();
}
public void end() {
popcorn.off();
theaterLight.bright();
screen.screeUp();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
package com.hao.demo.design.facade;
/**
* @author
* @date 2020-05-18
*/
public class Client {
public static void main(String[] args) {
// 方式一直接调用 麻烦不方便
// 方法二 外观模式
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
homeTheaterFacade.play();
homeTheaterFacade.pause();
homeTheaterFacade.end();
}
}