JAVA设计模式-外观模式
# JAVA设计模式-外观模式
# 介绍
外观模式是一种结构型模式,主要是为了隐藏系统的复杂性,对外提供一个可以访问的接口,客户端只需要访问这个接口即可。例如:我们现在的智能家居中的场景,平常我们下班回家,需要打开电灯,烧水、拉上窗帘、打开电视、打开空调,需要做很多事情,但是现在我们只需要回家之后在家里控制中心中设置为回家模式,对应的相关电器会自动执行相关指令,我们不需要关注具体的每一件事情,只需要设置场景为回家模式即可。JAVA开发中的三层开发模式也体现了外观模式。
# 优点
- 提高了安全性,客户端不需要知道系统的具体实现以及内部的复杂关系。
- 降低了客户端和子系统的耦合度,提高了灵活性,客户端不需要随着子系统的变化而变化,解决了客户端和子系统的高耦合。
# 缺点
- 不符合"开闭原则",需要修改时比较麻烦。
# 角色
- Facade:外观角色,该类知道哪些子系统负责哪些功能,其中的方法可能与一个或者多个子系统相关联,客户端请求传递到外观角色,再传递到子系统。
- Subsystem:子系统,实现系统部分具体功能的类。
- Client:客户端,负责调用外观角色。
# 代码示例
# ServiceAImpl
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 服务A-打开电灯
* @Author: xpy
* @Date: Created in 2022年10月06日 12:12 下午
*/
public class ServiceAImpl {
public void funA(){
System.out.println("打开电灯");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# ServiceBImpl
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 服务B-烧水
* @Author: xpy
* @Date: Created in 2022年10月06日 12:12 下午
*/
public class ServiceBImpl {
public void funB(){
System.out.println("烧水");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# ServiceCImpl
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 服务C-打开电视
* @Author: xpy
* @Date: Created in 2022年10月06日 12:12 下午
*/
public class ServiceCImpl {
public void funC(){
System.out.println("打开电视");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# ServiceDImpl
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 服务D-拉上窗帘
* @Author: xpy
* @Date: Created in 2022年10月06日 12:12 下午
*/
public class ServiceDImpl {
public void funD() {
System.out.println("拉上窗帘");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# ServiceEImpl
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 服务E-打开空调
* @Author: xpy
* @Date: Created in 2022年10月06日 12:12 下午
*/
public class ServiceEImpl {
public void funE() {
System.out.println("打开空调");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# SmartHomeFacade
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 外观角色
* @Author: xpy
* @Date: Created in 2022年10月06日 12:12 下午
*/
public class SmartHomeFacade {
private ServiceAImpl serviceA;
private ServiceBImpl serviceB;
private ServiceCImpl serviceC;
private ServiceDImpl serviceD;
private ServiceEImpl serviceE;
public SmartHomeFacade() {
this.serviceA = new ServiceAImpl();
this.serviceB = new ServiceBImpl();
this.serviceC = new ServiceCImpl();
this.serviceD = new ServiceDImpl();
this.serviceE = new ServiceEImpl();
}
public void goHome(){
System.out.println("回家模式");
serviceA.funA();
serviceB.funB();
serviceC.funC();
serviceD.funD();
serviceE.funE();
}
public void rest(){
System.out.println("放松模式");
serviceC.funC();
serviceD.funD();
serviceE.funE();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Client
/**
* All rights Reserved, Designed By monkey.blog.xpyvip.top
*
* @version V1.0
* @Package com.xpyvip.designpattern.adapter.facade
* @Description: 客户端
* @Author: xpy
* @Date: Created in 2022年10月06日 12:19 下午
*/
public class Client {
public static void main(String[] args) {
SmartHomeFacade smartHomeFacade = new SmartHomeFacade();
smartHomeFacade.goHome();
System.out.println("===============");
smartHomeFacade.rest();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 运行结果
# 扩展
# 开闭原则
"开闭原则"是在面向对象编程的领域中,规定“软件中的对象(类,模块,函数等等)应该对于扩展是开放的,但是对于修改是封闭的”,这意味着一个实体是允许在不改变它的源代码的前提下变更它的行为。
编辑 (opens new window)
上次更新: 2023/02/26, 10:03:01