- 定义:
Facade(外观)模式为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用。 -
UML:
- 模型:
家庭影院,包含DVD播放器,投影仪,自动屏幕,环绕立体声,爆米花机等,这些社保都有开关设置功能。最后我们用遥控器控制各个社保的开关,实现播放家庭影院:
实现播放家庭影院步骤:
1开爆米花机
2.放下屏幕
3.开投影仪
4.开音响
5.开DVD,选dvd
6.去拿爆米花
7.调节灯光
总体来看我们想播放家庭影院需要很多的复杂的步骤,并且这些步骤都是固定的。而对于用户并不关心这些步骤的操作过程,只要最后有爆米花,有的dvd看就行。外观模式就是做这种事情的。我们通过外观的包装,使用户只能看到外观对象,而不会看到具体的细节对象,这样无疑会降低应用程序的复杂度,并且提高了程序的可维护性。
public class DVDPlayer {
private static DVDPlayer instance = null;
private DVDPlayer() {}
public static DVDPlayer getInstance() {
if (instance == null) {
instance = new DVDPlayer();
}
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 pause");
}
public void setdvd() {
System.out.println("DVDPlayer is setting dvd");
}
}
//爆米花机
public class Popcorn {
private static Popcorn instance = null;
private Popcorn() {}
public static Popcorn getInstance() {
if (instance == null) {
instance = new Popcorn();
}
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");
}
}
//环绕立体声
public class Stereo {
private static Stereo instance = null;
private int volume = 5;
private Stereo() {}
public static Stereo getInstance() {
if (instance == null) {
instance = new Stereo();
}
return instance;
}
public void on() {
System.out.println("Stereo On");
}
public void off() {
System.out.println("Stereo Off");
}
public void setVolume(int vol) {
volume = vol;
System.out.println("the volume of Stereo is set to " + volume);
}
public void addVolume() {
if (volume < 11) {
volume++;
setVolume(volume);
}
}
public void subVolume() {
if (volume > 0) {
volume--;
setVolume(volume);
}
}
}
//投影机
public class Projector {
private int size=5;
private static Projector instance = null;
private Projector() {}
public static Projector getInstance() {
if (instance == null) {
instance = new Projector();
}
return instance;
}
public void on() {
System.out.println("Projector On");
}
public void off() {
System.out.println("Projector Off");
}
public void focus() {
System.out.println("Popcorn is focus");
}
public void zoom(int size) {
this.size=size;
System.out.println("Popcorn zoom to"+size);
}
}
//屏幕
public class Screen {
private static Screen instance = null;
private Screen() {}
public static Screen getInstance() {
if (instance == null) {
instance = new Screen();
}
return instance;
}
public void up() {
System.out.println("Screen up");
}
public void down() {
System.out.println("Screen down");
}
}
//灯
public class TheaterLights {
private static TheaterLights instance = null;
private TheaterLights() { }
public static TheaterLights getInstance() {
if (instance == null) {
instance = new TheaterLights();
}
return instance;
}
public void on() {
System.out.println("TheaterLights On");
}
public void off() {
System.out.println("TheaterLights Off");
}
//调节亮度
public void dim(int d) {
System.out.println("TheaterLights dim to " + d + "%");
}
public void bright() {
dim(100);
System.out.println("TheaterLights bright");
}
}
//包装的外观类
public class HomeTheaterFacade {
private TheaterLights mTheaterLights;
private Popcorn mPopcorn;
private Stereo mStereo;
private Projector mProjector;
private Screen mScreen;
private DVDPlayer mDVDPlayer;
//注入各个设备对象
public HomeTheaterFacade() {
mTheaterLights = TheaterLights.getInstance();
mPopcorn = Popcorn.getInstance();
mStereo = Stereo.getInstance();
mProjector = Projector.getInstance();
mScreen = Screen.getInstance();
mDVDPlayer = DVDPlayer.getInstance();
}
//固定的准备流程
public void ready() {
mPopcorn.on();
mPopcorn.pop();
mScreen.down();
mProjector.on();
mStereo.on();
mDVDPlayer.on();
mDVDPlayer.setdvd();
mTheaterLights.dim(10);
}
//固定的关闭流程
public void end() {
mPopcorn.off();
mTheaterLights.bright();
mScreen.up();
mProjector.off();
mStereo.off();
mDVDPlayer.setdvd();
mDVDPlayer.off();
}
public void play() {
mDVDPlayer.play();
}
public void pause() {
mDVDPlayer.pause();
}
}
使用包装的外观对象操作家庭影院:
public class MainTest {
public static void main(String[] args) {
HomeTheaterFacade mHomeTheaterFacade=new HomeTheaterFacade();
mHomeTheaterFacade.ready();
mHomeTheaterFacade.play();
}
}
通过这样的包装,我们可以把外观对象做成遥控器,这样我们就能通过遥控器来实现家庭影院的控制。