一、定义
外观模式为子系统中的一组接口提供一个一致的界面,其实就是提供一个高层接口,方便别人的调用。
二、框架运用
spring jdbc中的外观模式(org.springframework.jdbc.support.JdbcUtils
),Mybatis中的外观模式(org.apache.ibatis.session.Configuration
)
三、实战
1、子系统类
public class SuSystemOne {
public void methodOne(){
System.out.println("SuSystemOne out");
}
}
public class SuSystemTwo {
public void methodTwo(){
System.out.println("SuSystemTwo out");
}
}
public class SuSystemThree {
public void methodThree(){
System.out.println("SuSystemThree out");
}
}
2、外观类
public class Facade {
private SuSystemOne suSystemOne;
private SuSystemTwo suSystemTwo;
private SuSystemThree suSystemThree;
public Facade() {
this.suSystemOne = new SuSystemOne();
this.suSystemTwo = new SuSystemTwo();
this.suSystemThree = new SuSystemThree();
}
public void methodA(){
suSystemOne.methodOne();
suSystemTwo.methodTwo();
suSystemThree.methodThree();
System.out.println("--------end-------");
}
}
3、执行类
public class Client {
public static void main(String[] args) {
Facade facade = new Facade();
facade.methodA();
}
}
4、结果
Connected to the target VM, address: '127.0.0.1:53645', transport: 'socket'
SuSystemOne out
SuSystemTwo out
SuSystemThree out
--------end-------
Disconnected from the target VM, address: '127.0.0.1:53645', transport: 'socket'