外观模式(Facade)也叫过程模式, 外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节。
外观类(Facade): 为调用端提供统一的调用接口,外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当的子系统对象。
子系统的集合: 指模块或子系统,处理Facade对象指派的任务,他是功能的实际提供者。
外观模式在MyBatis框架中的应用:MyBatis中的Configuration去创建MetaObject对象使用到外观模式。
用外观模式模拟一个电影院
public class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();
private DVDPlayer(){}
public static DVDPlayer getInstance(){
return instance;
}
public void play(){
System.out.println(" dvd is playing ");
}
public void on(){
System.out.println(" dvd on ");
}
public void off(){
System.out.println(" dvd off ");
}
public void pause(){
System.out.println(" dvd pause ");
}
}
// 爆米花
public class Popcorn {
private static Popcorn instance = new Popcorn();
private Popcorn(){}
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 ");
}
}
// 投影机
public class Projector {
private static Projector instance = new Projector();
private Projector(){}
public static Projector getInstance(){
return instance;
}
public void on(){
System.out.println(" Projector on ");
}
public void off(){
System.out.println(" Projector off ");
}
public void focus(){
System.out.println(" Projector is focus ");
}
}
// 屏幕
public class Screen {
private static Screen instance = new Screen();
private Screen(){}
public static Screen getInstance(){
return instance;
}
public void up(){
System.out.println(" Screen up ");
}
public void down(){
System.out.println(" Screen down ");
}
}
// 影院立体声
public class Stereo {
private static Stereo instance = new Stereo();
private Stereo(){}
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 ");
}
}
// 灯光
public class TheaterLight {
private static TheaterLight instance = new TheaterLight();
private 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 ");
}
}
// 外观
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 read(){
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();
}
}
public class Client {
public static void main(String[] args){
HomeTheaterFacade facade = new HomeTheaterFacade();
facade.read();
facade.play();
facade.end();
}
}