外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
Hospital类
1 package top.bigking.facade;
2
3 /**
4 * @Author ABKing
5 * @since 2020/2/22 下午2:40
6 **/
7 public class School {
8 public School() {
9 System.out.println("学校");
10 }
11 }
School类
1 package top.bigking.facade;
2
3 /**
4 * @Author ABKing
5 * @since 2020/2/22 下午2:40
6 **/
7 public class School {
8 public School() {
9 System.out.println("学校");
10 }
11 }
Bank类
1 package top.bigking.facade;
2
3 /**
4 * @Author ABKing
5 * @since 2020/2/22 下午2:44
6 **/
7 public class Bank {
8 public Bank() {
9 System.out.println("银行");
10 }
11 }
门面类:
1 package top.bigking.facade;
2
3 /**
4 * @Author ABKing
5 * @since 2020/2/22 下午2:44
6 **/
7 public class RegisterFacade {
8 public static void register(){
9 Bank bank = new Bank();
10 Hospital hospital = new Hospital();
11 School school = new School();
12 }
13 }
单元测试:
1 package top.bigking.facade;
2
3 import org.junit.Test;
4
5 /**
6 * @Author ABKing
7 * @since 2020/2/22 下午2:46
8 **/
9 public class TestFacade {
10 @Test
11 public void testFacade(){
12 RegisterFacade.register();
13 }
14 }
开发常用的场景:
频率很高,哪里都会遇到。各种技术和框架中,都有外观模式的使用。如:
JDBC封装后的,commons提供的DBUtils类,Hibernate提供的工具类、Spring JDBC工具类。
来源:https://www.cnblogs.com/ABKing/p/12345426.html