关灯——放下屏幕——打开扬声器——打开DVD。
实现如下:
package javaapplication35;
public class Main {
public static void main(String[] args) {
Light light=new Light();
light.turnOff();
Screen screen=new Screen();
screen.down();
Amplifier amplifier=new Amplifier();
amplifier.On();
Dvdplayer player=new Dvdplayer();
player.On();
}
}
class Light {
public void turnOn() {
System.out.println("Lights Turn On");
}
public void turnOff() {
System.out.println("Lights Turn Off");
}
}
class Screen {
public void down() {
System.out.println("Put the Screen Down");
}
public void up() {
System.out.println("Put the Screen Up");
}
}
class Amplifier {
public void On() {
System.out.println("Turn Sound Amplifier On");
}
public void Off() {
System.out.println("Turn Sound Amplifier Off");
}
}
class Dvdplayer {
public void On() {
System.out.println("Turn the DVD player On");
}
public void Off() {
System.out.println("Turn the DVD player Off");
}
}
可以看到,想看部电影,在客户端(主程序)要进行N多操作后才行。其实如果设备更多,比如说还要调整音频属性,切换DVD专属频道,打开空调等。每次我想看部电影,就要重复执行这些命令。
可不可执行一次命令就可以搞定一切呢?很简单,专门写个类,写个函数实现这一串命令就可以了。客户端到时候就只要调用这个新增的类中的函数即可。
把主函数的过程直接移去到另一个类的函数中:
public class Main {
public static void main(String[] args) {
(new TheaterFacade()).WatchDvd();
}
}
class TheaterFacade {
public void WatchDvd() {
Light light = new Light();
light.turnOff();
Screen screen = new Screen();
screen.down();
Amplifier amplifier = new Amplifier();
amplifier.On();
Dvdplayer player = new Dvdplayer();
player.On();
}
}
这个就是最基本的Façade Pattern。前提条件是:当客户端要实现某个功能的时候,必须要调用一连串的类及其函数。
为了实现“一键”功能能,就在客户端和众多的类库(实现具体过程)中间,添加一个Façade类,类中的方法实现了各种不同的“一键功能”(一键打开家庭影院,一键关闭。。。)
这样做还有一个好处就是隔离了客户端和起具体实现作用的类库。
最后,上面的TheaterFacade类还可以写的更灵活点,因为上面的程序把实例化Light,Screen等放到了具体的方法内部,这样当我想“关家庭影院”时,因为找不到具体对象就没法操作了。
把实例化放到具体方法外面,并添加一个构造函数,以便可以指定特定的播放仪器(比如家里有两个扬声器的时候?)
package javaapplication35;
public class Main {
public static void main(String[] args) {
Light light = new Light();
Screen screen = new Screen();
Amplifier amplifier = new Amplifier();
Dvdplayer dvdplayer = new Dvdplayer();
TheaterFacade facade = new TheaterFacade(light, screen, amplifier, dvdplayer);
facade.WatchDvd();
facade.CloseDvd();
}
}
class TheaterFacade {
Light light;
Screen screen;
Amplifier amplifier;
Dvdplayer dvdplayer;
TheaterFacade(Light light, Screen screen, Amplifier amplifier, Dvdplayer dvdplayer) {
this.light = light;
this.screen = screen;
this.amplifier = amplifier;
this.dvdplayer = dvdplayer;
}
public void WatchDvd() {
light.turnOff();
screen.down();
amplifier.On();
dvdplayer.On();
}
public void CloseDvd() {
light.turnOn();
screen.up();
amplifier.Off();
dvdplayer.Off();
}
}
class Light {
public void turnOn() {
System.out.println("Lights Turn On");
}
public void turnOff() {
System.out.println("Lights Turn Off");
}
}
class Screen {
public void down() {
System.out.println("Put the Screen Down");
}
public void up() {
System.out.println("Put the Screen Up");
}
}
class Amplifier {
public void On() {
System.out.println("Turn Sound Amplifier On");
}
public void Off() {
System.out.println("Turn Sound Amplifier Off");
}
}
class Dvdplayer {
public void On() {
System.out.println("Turn the DVD player On");
}
public void Off() {
System.out.println("Turn the DVD player Off");
}
}