一、外观模式介绍
外观模式也叫过程模式。外观模式通常定义一个一致的接口,用于屏蔽内部子系统的细节,使得调用端只需跟这个接口发行调用,而无需关心这个子系统的内部细节。
二、外观模式原理
外观模式原理类图如下所示:
说明:
1)外观类(Facade):为调用端提供统一的调用接口,外观类知道哪些子系统负责处理哪些请求,从而将调用端的请求代理给适当子系统对象
2)调用者(Client):外观接口的调用者
3)子系统的集合:子模块或子系统,处理Facade对象指派的任务,它是功能的提供者
三、外观模式例子
实现组建一个家庭影院管理项目:DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花,要求完成使用家庭影院的功能,其过程为:1)直接用遥控器:统筹各设备开关;2)开爆米花机;3)放下屏幕;4)开投影仪;5)开音响;6)开DVD,选节目;7)去拿爆米花;8)调暗灯光;9)播放;10)观影结束,关闭各种设备。
用传统方式解决影院管理,示意图如下所示:
在ClientTest的main方法中,创建各个子系统的对象,并直接去调用子系统(对象)相关方法,会造成调用过程混乱,没有清晰的过程,不利于在ClientTest中去为维护子系统的操作。为了解决上述问题,可以定义一个高层接口,给子系统中的一组接口提供一个一致的界面(比如在高层接口提供四个方法:ready、play、pause、end),用来访问子系统中的一群接口。也就是说,通过定义一个一致的接口,用于屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而不需知道这个子系统的内部细节,即使用外观模式来实现。
使用外观模式实现家庭影院管理项目类图如下所示:
代码如下图所示:
创建DVDPlayer类:
public class DVDPlayer{
private DVDPlayer(){}
private static final volatile DVDPlayer INSTANCE = null;
//使用单例模式获取对象
public static DVDPlayer getInstance{
if(null == instance){
synchronized(DVDPlayer.class){
if(null == instance){
INSTANCE = new DVDPlayer();
}
}
}
}
public void on(){
System.out.println("DVD 打开中...");
}
public void off(){
System.out.println("DVD 关闭中...");
}
public void play(){
System.out.println("DVD 播放中...");
}
public void pause(){
System.out.println("DVD 暂停中...");
}
}
创建Popcorn类:
public class Popcorn{
private Popcorn(){}
private static final volatile Popcorn INSTANCE = null;
//使用单例模式获取对象
public static Popcorn getInstance{
if(null == instance){
synchronized(Popcorn.class){
if(null == instance){
INSTANCE = new Popcorn();
}
}
}
}
public void on(){
System.out.println("Popcorn 打开中...");
}
public void off(){
System.out.println("Popcorn 关闭中...");
}
public void pop(){
System.out.println("DVD poping...");
}
}
创建Projector类:
public class Projector{
private Projector(){}
private static final volatile Projector INSTANCE = null;
//使用单例模式获取对象
public static Projector getInstance{
if(null == instance){
synchronized(Projector.class){
if(null == instance){
INSTANCE = new Projector();
}
}
}
}
public void on(){
System.out.println("Projector 打开中...");
}
public void off(){
System.out.println("Projector 关闭中...");
}
public void focus(){
System.out.println("Projector is Projector...");
}
}
创建Screen类:
public class Screen{
private Screen(){}
private static final volatile Screen INSTANCE = null;
//使用单例模式获取对象
public static Screen getInstance{
if(null == instance){
synchronized(Screen.class){
if(null == instance){
INSTANCE = new Screen();
}
}
}
}
public void up(){
System.out.println("Screen 打开中...");
}
public void down(){
System.out.println("Screen 关闭中...");
}
}
创建Stereo类:
public class Stereo{
private Stereo(){}
private static final volatile Stereo INSTANCE = null;
//使用单例模式获取对象
public static Stereo getInstance{
if(null == instance){
synchronized(Stereo.class){
if(null == instance){
INSTANCE = new Stereo();
}
}
}
}
public void on(){
System.out.println("Screen 打开中...");
}
public void off(){
System.out.println("Stereo 关闭中...");
}
public void up(){
System.out.println("Screen up中...");
}
}
创建TheatherLight类:
public class TheaterLight{
private TheatherLight(){}
private static final volatile TheaterLight INSTANCE = null;
//使用单例模式获取对象
public static TheaterLight getInstance{
if(null == instance){
synchronized(TheaterLight .class){
if(null == instance){
INSTANCE = new TheaterLight();
}
}
}
}
public void on(){
System.out.println("TheaterLight 打开中...");
}
public void off(){
System.out.println("TheaterLight 关闭中...");
}
public void dim(){
System.out.println("TheaterLight dim中...");
}
public void bright(){
System.out.println("TheaterLight bright中...");
}
}
创建HomeTheaterFacade类:
public class HomeTheaterFacade{
private TheaterLight theaterLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dvdPlayer;
public HomeTheaterFacade(){
theaterLight = TheaterLight.getInstance();
popcorn= Popcorn.getInstance();
projector= Projector.getInstance();
screen= Screen.getInstance();
dvdPlayer= DVDPlayer.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();
}
}
创建Client类:
public class Client{
public static void main(String[] args){
HomeTheaterFacade facade = new HomeTheaterFacade();
facade.ready();
facade.play();
facade.end();
}
}
四、外观模式注意事项和细节
1)外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
2)外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展
3)通过合理的使用外观模式,可以帮我们更好的划分访问的层次
4)当系统需要进行分层设计时,可以考虑使用Facade模式
5)在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性
6)不能过多的或不合理的使用外观模式,使用外观模式好,还是直接调用模块,要以让系统有层次,利于维护为目的