外观模式:提供了一个统一的接口,用来访问子系统中的一大堆接口,外观定义了一个高层接口,让子系统更容易使用。
意图: 让接口更加简单。
public interface Light {
void on();
void off();
void dim();
}
public interface Screen {
void up();
void down();
}
public interface DvdPlayer {
void on();
void off();
void pause();
}
/**
* 外观模式类
* Created by Administrator on 2018/1/9.
*/
public class HomeTheaterFacade {
private Screen screen;
private DvdPlayer dvdPlayer;
private Light light;
public HomeTheaterFacade(Screen screen, DvdPlayer dvdPlayer, Light light) {
this.screen = screen;
this.dvdPlayer = dvdPlayer;
this.light = light;
}
/**
* 外观模式,
* 将众多复杂的子模块封装起来,提供一个统一的调用
* **/
public void watchMovie(){
screen.down();
dvdPlayer.on();
light.dim();
}
public void endMovie(){
screen.up();
dvdPlayer.off();
light.on();
}
}
public class TestFacade {
public static void main(String[] args) {
// screen ,light 通过各子类实现传入构造方法
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(screen,dvdplayer,light);
homeTheaterFacade.watchMovie();
homeTheaterFacade.endMovie();
}
}