外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。 外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节。二、类图
说明:
(1)外观类(Facade):为调用端提供统一的调用接口,外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当的子系统对象
(2)调用者(Client):外观接口的调用者
(3)子系统:处理Facade对象指派的任务,功能的实际提供者
DVDPlayer.java
public class DVDPlayer {
// 单例模式(饿汉式)
// 1.私有化构造器
private DVDPlayer() {
}
// 2.类的内部创建对象
private static DVDPlayer instance = new DVDPlayer();
// 3. 提供公共的静态方法,返回实例对象
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.java
public class Popcorn {
// 单例模式(饿汉式)
// 1.私有化构造器
private Popcorn() {
}
// 2.类的内部创建对象
private static Popcorn instance = new Popcorn();
// 3. 提供公共的静态方法,返回实例对象
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. ");
}
}
Projector.java
public class Projector {
// 单例模式(饿汉式)
// 1.私有化构造器
private Projector() {
}
// 2.类的内部创建对象
private static Projector instance = new Projector();
// 3. 提供公共的静态方法,返回实例对象
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println(" Projector on. ");
}
public void off() {
System.out.println(" Projector off. ");
}
public void facus() {
System.out.println(" Projector is facusing. ");
}
}
Screen.java
public class Screen {
// 单例模式(饿汉式)
// 1.私有化构造器
private Screen() {
}
// 2.类的内部创建对象
private static Screen instance = new Screen();
// 3. 提供公共的静态方法,返回实例对象
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println(" Screen up. ");
}
public void down() {
System.out.println(" Screen down. ");
}
}
Stereo.java
public class Stereo {
// 单例模式(饿汉式)
// 1.私有化构造器
private Stereo() {
}
// 2.类的内部创建对象
private static Stereo instance = new Stereo();
// 3. 提供公共的静态方法,返回实例对象
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(" Stereo up. ");
}
public void down() {
System.out.println(" Stereo down. ");
}
}
TheaterLight.java
public class TheaterLight {
// 单例模式(饿汉式)
// 1.私有化构造器
private TheaterLight() {
}
// 2.类的内部创建对象
private static TheaterLight instance = new TheaterLight();
// 3. 提供公共的静态方法,返回实例对象
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. ");
}
}
2.外观类
HomeTheaterFacade.java
public class HomeTheaterFacade {
// 定义各个子系统的对象
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Screen screen;
private Stereo stereo;
private TheaterLight theaterLight;
// 构造器
public HomeTheaterFacade() {
super();
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.pop();
screen.down();
projector.on();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}
public void play() {
dvdPlayer.play();
}
public void pause() {
dvdPlayer.pause();
}
public void end() {
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
3.客户端
Client.java
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
System.out.println("**********");
homeTheaterFacade.play();
System.out.println("**********");
homeTheaterFacade.pause();
System.out.println("**********");
homeTheaterFacade.end();
}



