深入理解Java设计模式之外观模式
作者:一指流砂~ 时间:2023-05-02 09:48:00
一、什么是外观模式
定义:为子系统中的一组接口提供一个一致的界面,用来访问子系统中的一群接口。
外观模式组成:
Facade
:负责子系统的的封装调用
Subsystem Classes
:具体的子系统,实现由外观模式Facade对象来调用的具体任务
二、外观模式的使用场景
1、设计初期阶段,应该有意识的将不同层分离,层与层之间建立外观模式;
2、开发阶段,子系统越来越复杂,增加外观模式提供一个简单的调用接口;
3、维护一个大型遗留系统的时候,可能这个系统已经非常难以维护和扩展,但又包含非常重要的功能,为其开发一个外观类,以便新系统与其交互。
三、外观模式的优缺点
优点:
1、实现了子系统与客户端之间的松耦合关系;
2、客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使得子系统使用起来更加容易。
缺点:
1、不符合开闭原则,如果要修改某一个子系统的功能,通常外观类也要一起修改;
2、没有办法直接阻止外部不通过外观类访问子系统的功能,因为子系统类中的功能必须是公开的(根据需要决定是否使用internal访问级别可解决这个缺点,但外观类需要和子系统类在同一个程序集内)。
四、外观模式的实现
先写出四个子系统的类
class SubSystemOne
{
public void MethodOne()
{
Console.WriteLine("子系统方法一");
}
}
class SubSystemTwo
{
public void MethodTwo()
{
Console.WriteLine("子系统方法二");
}
}
class SubSystemThree
{
public void MethodThree()
{
Console.WriteLine("子系统方法三");
}
}
class SubSystemFour
{
public void MethodFour()
{
Console.WriteLine("子系统犯法四");
}
}
引入外观类,减少子系统类之间的交互
class Facade
{
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade()
{
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
public void MethodA()
{
Console.WriteLine("\n方法组合A()---");
one.MethodOne();
two.MethodTwo();
four.MethodFour();
}
public void MethodB()
{
Console.WriteLine("\n方法组B()---");
two.MethodTwo();
three.MethodThree();
}
}
客户端代码:
static void Main(string[] args)
{
Facade facade = new Facade();
facade.MethodA();
facade.MethodB();
Console.Read();
}
来源:https://www.cnblogs.com/xuwendong/p/10414861.html
标签:Java,设计模式,外观模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
流读取导致StringBuilder.toString()乱码的问题及解决
2022-12-20 13:34:14
在web.config和app.config文件中增加自定义配置节点的方法
2021-11-03 03:21:41
C#实现伪装文件夹功能
2023-04-28 14:21:38
![](https://img.aspxhome.com/file/2023/0/76410_0s.png)
Android实现点击图片上传SQLite数据库
2021-06-23 04:35:50
详解spring cloud中使用Ribbon实现客户端的软负载均衡
2022-10-26 15:54:45
![](https://img.aspxhome.com/file/2023/4/110834_0s.png)
c#基于Win32Api实现返回Windows桌面功能
2022-11-21 15:29:51
Sentinel源码解析入口类和SlotChain构建过程详解
2022-06-14 18:29:50
![](https://img.aspxhome.com/file/2023/2/105972_0s.png)
C#字体池技术实现代码详解
2022-12-30 06:18:02
javaweb实现app扫码登录功能
2022-03-25 03:48:19
SpringCloud迈向云原生的步骤
2023-08-21 22:59:46
![](https://img.aspxhome.com/file/2023/2/95032_0s.png)
http协议进阶之Transfer-Encoding和HttpCore实现详解
2023-11-03 14:22:05
![](https://img.aspxhome.com/file/2023/2/87212_0s.png)
C#实现的Win32控制台线程计时器功能示例
2022-12-19 20:29:20
Java编程中随机数的生成方式总结
2022-06-14 11:57:27
java实现马踏棋盘算法(骑士周游问题)
2022-03-17 20:29:46
![](https://img.aspxhome.com/file/2023/1/94361_0s.jpg)
springboot使用Logback把日志输出到控制台或输出到文件
2022-05-30 17:34:30
![](https://img.aspxhome.com/file/2023/3/63763_0s.png)
zookeeper实现分布式锁
2023-08-03 13:44:18
![](https://img.aspxhome.com/file/2023/6/79346_0s.jpg)
Android通过Movie展示Gif格式图片
2023-06-16 19:29:38
Flutter进阶之实现动画效果(九)
2023-04-11 12:20:06
![](https://img.aspxhome.com/file/2023/5/138185_0s.gif)
Java实现人脸识别登录、注册等功能(最新完整版)
2022-09-25 00:20:13
![](https://img.aspxhome.com/file/2023/2/75662_0s.gif)
Springboot如何获取yml、properties参数
2021-09-20 17:34:43
![](https://img.aspxhome.com/file/2023/3/129233_0s.jpg)