结构型——外观模式(Facade)
外观模式基本介绍
- 外观模式(Facade),外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
- 外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节
一个简单的例子理解:去一些政府部门办事,会跑很多不同的部门,可能去了部门A,部门A又让你去部门B……这样就很麻烦
如果使用门面模式,用户只需要访问门面,由门面模式去协调内部的关系。
外观模式原理类图
➢对类图说明(分类外观模式的角色)
- 外观类(Facade):为调用端提供统一的调用接口,外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当子系统对象
- 调用者(Client):外观接口的调用者
- 子系统的集合:指模块或者子系统,处理Facade对象指派的任务,他是功能的实际提供者。
影院管理项目
组建一个家庭影院:
DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的功能,其过程为:
直接用遥控器:统筹各设备开关
开爆米花机
放下屏幕
开投影仪
开音响
开DVD,选dvd
去拿爆米花
调暗灯光
播放
观影结束后,关闭各种设备
传统方式
分析
- 在ClientTest的main方法中,创建各个子系统的对象,并直接去调用子系统(对象)相关方法,会造成调用过程混乱,没有清晰的过程
- 不利于在ClientTest中,去维护对子系统的操作
- 解决思路: 定义一个高层接口,给子系统中的一组接口提供一个一致的界面(比如在高层接口提供四个方法(ready, play, pause, end),用来访问子系统中的一群接口
- 也就是说 就是通过定义-一个- -致的接口(界面类),用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节=>外观模式
外观模式
- 外观模式可以理解为转换一群接口, 客户只要调用一个接口,而不用调用多个接口才能达到目的。比如:在pc上安装软件的时候经常有一键安装选项(省去选择安装目录、安装的组件等等),还有就是手机的重启功能(把.关机和启动合为一个操作)。
- 外观模式就是解决多个复杂接口带来的使用困难,起到简化用户操作的作用
- 示意图说明
package com.xhl.Facade;
public class DVDPlays {
//单例模式,饿汉
private static final DVDPlays instance = new DVDPlays();
private DVDPlays() {};
public static DVDPlays getIntance(){
return instance;
}
public void on() {
System.out.println("DVD ON");
}
public void off() {
System.out.println("DVD OFF");
}
public void play() {
System.out.println("DVD IS PLAYING");
}
public void pause() {
System.out.println("DVD PAUSE...");
}
}
package com.xhl.Facade;
public class Popcorn {
//单例模式,饿汉
private static final Popcorn instance = new Popcorn();
private Popcorn() {};
public static Popcorn getIntance(){
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");
}
}
package com.xhl.Facade;
public class Projector {
//单例模式,饿汉
private static final Projector instance = new Projector();
private Projector() {};
public static Projector getIntance(){
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 PROJECTOR");
}
}
package com.xhl.Facade;
public class Screen {
//单例模式,饿汉
private static final Screen instance = new Screen();
private Screen() {};
public static Screen getIntance(){
return instance;
}
public void up() {
System.out.println("SCREEN UP");
}
public void down() {
System.out.println("SCREEN DOWN");
}
}
package com.xhl.Facade;
public class Stereo {
//单例模式,饿汉
private static final Stereo instance = new Stereo();
private Stereo() {};
public static Stereo getIntance(){
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...");
}
}
package com.xhl.Facade;
public class TheaterLight {
//单例模式,饿汉
private static final TheaterLight instance = new TheaterLight();
private TheaterLight() {};
public static TheaterLight getIntance(){
return instance;
}
public void on() {
System.out.println("THEATER LIGHT ON");
}
public void off() {
System.out.println("THEATER LIGHT OFF");
}
public void dim() {
System.out.println("THEATER LIGHT DIM...");
}
public void brigth() {
System.out.println("THEATER LIGHT BRIGHT...");
}
}
package com.xhl.Facade;
public class HomeTheaterFacade {
private TheaterLight theaterLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlays dvdPlays;
public HomeTheaterFacade() {
super();
this.theaterLight = TheaterLight.getIntance();
this.popcorn = Popcorn.getIntance();
this.stereo = Stereo.getIntance();
this.projector = Projector.getIntance();
this.screen = Screen.getIntance();
this.dvdPlays = DVDPlays.getIntance();
}
public void ready() {
popcorn.on();
popcorn.pop();
screen.down();
projector.on();
stereo.on();
dvdPlays.on();
theaterLight.dim();
}
public void play() {
dvdPlays.play();
}
public void pause() {
dvdPlays.pause();
}
public void end() {
popcorn.off();
theaterLight.off();
screen.up();
projector.off();
stereo.off();
dvdPlays.off();
}
}
package com.xhl.Facade;
public class FacadeDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
HomeTheaterFacade facade = new HomeTheaterFacade();
facade.ready();
System.out.println("=============================");
facade.play();
System.out.println("=============================");
facade.pause();
System.out.println("=============================");
facade.end();
}
}
外观模式在MyBatista框架中的源码分析
MyBatis中Configuration去创建MetaObject对象使用外观模式
Configuration是一个外观类,他聚合了objectFactory objectWrapperFactory reflectFactory对象实例
总结
- 外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
- 外观模式对客户端与子 系统的耦合关系-------解耦,让子系统内部的模块更易维护和扩展
- 通过合理的使用外观模式,可以帮我们更好的划分访问的层次
- 当系统需要进行分层设计时,可以考虑使用Facade模式
- 在维护一-个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性
- 不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利于维护为目的。
行为型——中介者模式(Mediator)
中介者模式基本介绍
- 中介者模式(Mediator Pattern) ,用一个中介对象来封装一 系列的对象交互。中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互
- 中介者模式属于行为型模式,使代码易于维护
- 比如 MVC模式,C (Controller 控制器)是M (Model 模型)和V (View 视图)的中介者,在前后端交互时起到了中间人的作用。消息中间件就是中介者。
还是之前的例子,政府部门内部打交道也是很复杂,一个新的部门进来,十分麻烦,我们可以抽出一个专门的部门打交道,所有部门都和这个部门打交道,互相之间不打交道,中介者模式是对抽出一个部门,外观模式是对外抽出一个部门,这个对内对外的部门可以是同一个。
中介者模式原理类图
- Mediator 就是抽象中介者,定义了同事对象到中介者对象的接口
- Colleague 是抽象同事类
- ConcreteMediator 具体的中介者对象,实现抽象方法,他需要知道所有的具体的同事类,即以一个集合来管理HashMap,并接受某个同事对象消息,完成相应的任务
- ConcreteColleague 具体的同事类,会有很多,每个同事只知道自己的行为,而不了解其他同事类的行为(方法), 但是他们都依赖中介者对 象.
智能家庭项目
- 智能家庭包括各种设备,闹钟、咖啡机、电视机、窗帘等
- 主人要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流程为:闹铃响起----->咖啡机开始做咖啡----->窗帘自动落下----->电视机开始播放
传统方式
分析
- 当各电器对象有多种状态改变时,相互之.间的调用关系会比较复杂
- 各个电器对象彼此联系,你中有我,我中有你,不利于松耦合.
- 各个电器对象之间所传递的消息(参数),容易混乱
- 当系统增加一个新的电器对象时,或者执行流程改变时,代码的可维护性、扩展性都不理想==>考虑中介者模式
中介者模式
package com.xhl.Mediator;
//同事抽象类
public abstract class Colleague {
private Mediator mediator;
public String name;
public Colleague(Mediator mediator, String name) {
super();
this.mediator = mediator;
this.name = name;
}
public Mediator GetMediator() {
return this.mediator;
}
public abstract void SendMessage(int stateChange);
}
package com.xhl.Mediator;
public class Alarm extends Colleague {
public Alarm(Mediator mediator, String name) {
super(mediator, name);
//在创建Alarm同事对象时,将自己放到ConcrateMediator集合中
mediator.Register(name, this);
}
public void SendAlarm(int stateChange) {
SendMessage(stateChange);
}
@Override
public void SendMessage(int stateChange) {
// 调用的中介者对象的getMessage
this.GetMediator().GetMessage(stateChange, this.name);
}
}
package com.xhl.Mediator;
public class CoffeeMachine extends Colleague {
public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
//在创建Alarm同事对象时,将自己放到ConcrateMediator集合中
mediator.Register(name, this);
}
public void StartCoffee() {
System.out.println("It's time to startcoffee!");
}
@Override
public void SendMessage(int stateChange) {
// 调用的中介者对象的getMessage
this.GetMediator().GetMessage(stateChange, this.name);
}
public void FinishCoffee() {
System.out.println("After 5 minutes!");
System.out.println("Coffee is ok");
SendMessage(0);
}
}
package com.xhl.Mediator;
public class Curtains extends Colleague {
public Curtains(Mediator mediator, String name) {
super(mediator, name);
//在创建Alarm同事对象时,将自己放到ConcrateMediator集合中
mediator.Register(name, this);
}
public void UpCurtain() {
System.out.println("I am holding Up Curtains!");
}
@Override
public void SendMessage(int stateChange) {
// 调用的中介者对象的getMessage
this.GetMediator().GetMessage(stateChange, this.name);
}
}
package com.xhl.Mediator;
public class TV extends Colleague {
public TV(Mediator mediator, String name) {
super(mediator, name);
//在创建Alarm同事对象时,将自己放到ConcrateMediator集合中
mediator.Register(name, this);
}
public void StartTV() {
System.out.println("It's time to StartTV!");
}
@Override
public void SendMessage(int stateChange) {
// 调用的中介者对象的getMessage
this.GetMediator().GetMessage(stateChange, this.name);
}
public void StopTV() {
System.out.println("StopTV!");
}
}
package com.xhl.Mediator;
public abstract class Mediator {
//将给中介者对象加入到集合中
public abstract void Register(String name,Colleague colleague);
//接受信息,具体的同事对象发出
public abstract void GetMessage(int stateChange , String name);
public abstract void SendMessage();
}
package com.xhl.Mediator;
import java.util.HashMap;
public class ConcreteMediator extends Mediator {
//集合放入所以的同事对象
private HashMap<String, Colleague> colleagueMap;
private HashMap<String, String> interMap;
public ConcreteMediator() {
super();
colleagueMap = new HashMap();
interMap = new HashMap();
}
@Override
public void Register(String name, Colleague colleague) {
colleagueMap.put(name, colleague);
if(colleague instanceof Alarm) {
interMap.put("Alarm", name);
}else if(colleague instanceof CoffeeMachine) {
interMap.put("CoffeeMachine", name);
}else if(colleague instanceof TV) {
interMap.put("TV", name);
}else if(colleague instanceof Curtains) {
interMap.put("Curtains", name);
}
}
//集体中介者的核心方法
//1、根据得到信息,完成对应任务
//2、中介者在这个方法协调各个具体的同事对象,完成任务
@Override
public void GetMessage(int stateChange, String name) {
if(colleagueMap.get(name) instanceof Alarm) {
if(stateChange==0) {
((CoffeeMachine)(colleagueMap.get(interMap.get("CoffeeMachine")))).StartCoffee();
((TV)(colleagueMap.get(interMap.get("TV")))).StartTV();
}else if(stateChange==1) {
((TV)(colleagueMap.get(interMap.get("TV")))).StopTV();
}
}else if(colleagueMap.get(name) instanceof CoffeeMachine) {
((Curtains)(colleagueMap.get(interMap.get("Curtains")))).UpCurtain();
}else if(colleagueMap.get(name) instanceof TV) {
//TV发出的消息处理
}else if(colleagueMap.get(name) instanceof Curtains) {
//Curtains发出的消息处理
}
}
@Override
public void SendMessage() {
// TODO Auto-generated method stub
}
}
package com.xhl.Mediator;
public class MediatorDemo {
public static void main(String[] args) {
Mediator mediator = new ConcreteMediator();
Alarm alarm = new Alarm(mediator, "alarm");
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeeMachine");
Curtains curtains = new Curtains(mediator, "curtains");
TV tv = new TV(mediator, "TV");
alarm.SendAlarm(0);
coffeeMachine.FinishCoffee();
alarm.SendAlarm(1);
}
}
总结
1)多个类相互耦合,会形成网状结构,使用中介者模式将网状结构分离为星型结构,进行解耦
2)减少类间依赖,降低了耦合,符合迪米特原则
3)中介者承担 了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
4)如果设计不当, 中介者对象本身变得过于复杂,这点在实际使用时,要特别注意