外观模式:让子系统变得简单易懂
公号:码农充电站pro
主页:https://codeshellme.github.io
今天来介绍外观模式(Facade Design Pattern)。
1,外观模式
外观模式又叫门面模式,它提供了一个统一的(高层)接口,用来访问子系统中的一群接口,使得子系统更容易使用。
外观模式的类图如下:
Facede 简化了原来的非常复杂的子系统,使得子系统更易使用,Client 只需依赖 Facede 即可。
虽然有了 Facede ,但 Facede 并影响原来的子系统,也就是其它客户依然可以使用原有系统。
2,最少知识原则
最少知识原则告诉我们,应该尽量减少对象之间的交互,只与自己最需要的接口建立关系。
在设计系统时,不应该让太多的类耦合在一起,以免一小部分的改动,而影响到整个系统的使用。
这也就是我们俗话说的:牵一发而动全身,最少知识原则的就是让我们避免这种情况的发生。
外观模式则使用了最少知识原则。
3,外观模式举例
下面举一个简单的例子,来体会下外观模式的使用。俗话说:民以食为天。我们来举一个吃饭的例子。
话说,小明每次吃饭都要经过以下步骤:
- 去超市买菜。
- 回到家后,洗菜。
- 炒菜。
- 煮饭。
- 将饭菜盛到碗里。
- 吃饭。
- 洗碗。
可见小明吃一顿饭真的很麻烦。
我们用代码来模拟上面的过程,首先创建了三个类,分别是关于菜,饭,碗的操作:
class Vegetables {
public void bugVegetables() {
System.out.println("buying vegetables.");
}
public void washVegetables() {
System.out.println("washing vegetables.");
}
public void fryVegetables() {
System.out.println("frying vegetables.");
}
public void toBowl() {
System.out.println("putting the vegetables into the bowl.");
}
}
class Rice {
public void fryRice() {
System.out.println("frying rice.");
}
public void toBowl() {
System.out.println("putting the rice into the bowl.");
}
}
class Bowl {
private Vegetables vegetables;
private Rice rice;
public Bowl(Vegetables vegetables, Rice rice) {
this.vegetables = vegetables;
this.rice = rice;
}
// 盛好饭菜
public void prepare() {
vegetables.toBowl();
rice.toBowl();
}
public void washBowl() {
System.out.println("washing bowl.");
}
}
小明每次吃饭都需要与上面三个类做交互,而且需要很多步骤,如下:
Vegetables v = new Vegetables();
v.bugVegetables();
v.washVegetables();
v.fryVegetables();
Rice r = new Rice();
r.fryRice();
Bowl b = new Bowl(v, r);
b.prepare();
System.out.println("xiao ming is having a meal.");
b.washBowl();
后来,小明请了一位保姆,来帮他做饭洗碗等。所以我们创建了 Nanny
类:
class Nanny {
private Vegetables v;
private Rice r;
private Bowl b;
public Nanny() {
v = new Vegetables();
r = new Rice();
b = new Bowl(v, r);
}
public void prepareMeal() {
v.bugVegetables();
v.washVegetables();
v.fryVegetables();
r.fryRice();
b.prepare();
}
public void cleanUp() {
b.washBowl();
}
}
这样,小明再吃饭的时候就只需要跟保姆说一声,保姆就帮他做好了所有的事情:
Nanny n = new Nanny();
n.prepareMeal();
System.out.println("xiao ming is having a meal.");
n.cleanUp();
这样大大简化了小明吃饭的步骤,也节约了很多时间。
我将完整的代码放在了这里,供大家参考。
4,总结
外观模式主要用于简化系统的复杂度,为客户提供更易使用的接口,减少客户对复杂系统的依赖。
从外观模式中我们也能看到最少知识原则的运用。
(本节完。)
推荐阅读:
策略模式-定义一个算法族
观察者模式-将消息通知给观察者
装饰者模式-动态的包装原有对象的行为
命令模式-将请求封装成对象
适配器模式-让不兼容的接口得以适配
欢迎关注作者公众号,获取更多技术干货。
推荐阅读
-
简单易懂!让你轻松掌握JavaScript策略模式
-
外观模式:让子系统变得简单易懂
-
外观模式:简单易懂的例子解析 - 了解其定义
-
外观模式:让复杂系统变得简单易用
-
外观模式:让你的设计更加简洁易懂
-
外观模式:让复杂系统变得简单易用
-
大聪明教你学Java|Mybatis的一级缓存和二级缓存--🍊作者简介:不愿过河东,一个来自二线城市的程序员,致力于用 "猥琐 "的方法解决琐碎的问题,让复杂的问题变得简单易懂。支持作者:喜欢👍,关注💖,留言💌~! 前言。 在计算机世界中,缓存无处不在;操作系统有操作系统缓存,数据库会有数据库缓存,我们还可以利用中间件(如 Redis)来充当缓存。MyBatis 作为一个优秀的 ORM 框架,也用于缓存,所以今天我们就来谈谈 Mybatis 的一级缓存和二级缓存。 Mybatis 一级缓存 首先,我们来看一张图片👇。 我们在开发项目的过程中,如果打开Mybatis的SQL语句打印,经常会看到这样一句话:创建一个新的 SqlSession,其实这就是我们常说的 Mybatis 一级缓存。 Mybatis 的一级缓存也就是在执行一次 SQL 查询或 SQL 更新后,这条 SQL 语句并不会消失,而是被 MyBatis 缓存起来,当再次执行同样的 SQL 语句时,就会直接从缓存中提取出来,而不用再次执行 SQL 命令。第一级缓存也称为 SqlSession 级缓存。对数据库进行操作时,需要构建一个 SqlSession 对象,其中有一个用于存储缓存数据的数据结构(HashMap)。对象中有一个用于存储缓存数据的数据结构(HashMap)。不同 SqlSession 之间的缓存数据区域(HashMap)互不影响。 在我们的应用系统运行过程中,我们可能会在一个数据库会话中,执行多条查询条件相同的 SQL 语句,那么对于这种情况,你来进行设计,那么你会如何考虑呢?没错,就是加入缓存,MyBatis也是这么来处理的,如果是同一条SQL语句,会优先打入一级缓存,避免直接查询数据库,给数据库造成压力,提高性能。具体实现过程如下图所示👇 SqlSession 是一个接口,提供了一些 CRUD 方法,SqlSession 的默认实现类是 DefaultSqlSession,DefaultSqlSession 类持有 Executor 接口对象,Executor 的默认实现是 BaseExecutor 对象,每个 BaseExecutor 对象都有一个 PerpetualCache 缓存,即上图中的本地缓存。当用户发起查询时,MyBatis 会根据当前执行的语句生成一个 MappedStatement,并在本地缓存中进行查询,如果缓存被命中,查询结果会直接返回给用户;如果缓存未被命中,查询结果会直接返回给用户。如果缓存未命中,则查询数据库,将结果写入本地缓存,最后将结果返回给用户。这时候可能有小伙伴要说了:我还在控制台中看到 "关闭非事务性 SqlSession "这句话,说明我每次创建一个 SqlSession 到 SqlSession 结束都是关闭的,那么我的缓存还是有毛线用!😥 事情肯定不是我们想的那样,让我们继续👇。 🍊 getSqlSession 源代码
-
.NET高级面试指南 Topic XVIII [ 介绍外观模式(Appearance Pattern),该模式提供了一个隐藏系统复杂性的简化界面 ]。- 简化复杂系统:当系统具有复杂的子系统结构时,可以使用外观模式来简化界面。提供统一界面:当客户端需要访问多个子系统时,可以使用外观模式提供统一界面。 外观模式在现代软件开发中得到广泛应用,尤其是在复杂系统中。例如 图形用户界面库:许多图形用户界面库(如 Qt、GTK+ 等)都使用外观模式来隐藏底层的复杂性,并为开发人员提供简单的界面来创建用户界面。 操作系统接口:操作系统中的系统调用和应用程序接口通常也使用外观模式来隐藏底层硬件和系统的复杂性,为应用程序提供访问系统资源的简单接口。企业应用程序:在可能涉及多个子系统的大型企业应用程序中,外观模式可用于封装这些子系统,并为客户端提供统一的使用界面。 网络框架:许多网络框架(如 ASP.NET MVC、Spring MVC 等)也使用外观模式来隐藏底层的复杂性,并为开发人员提供简单的接口来处理 HTTP 请求和响应。 集成开发环境(IDE):集成开发环境通常包含代码编辑器、编译器、调试器等多种功能。外观模式可用于封装这些功能,并为开发人员提供开发软件的简单界面。 代码示例: