Design Patterns Uncovered: The Facade Pattern

本文聚焦于Facade模式的探讨。通过实例分析,解释了Facade如何在软件架构中提供一个统一接口,简化复杂子系统的使用。文章还讨论了Facade在实际世界中的应用,如操作系统的简化接口和建筑外观的简化设计。最后,通过Java代码示例展示了如何在实际项目中应用Facade模式,以提高代码的可读性和维护性。

This article will focus on the Facade pattern. So far in our design patterns we've already looked at the Observer and Adapterpatterns. Facade has some similarities with the Adapter, so it's a logical next step in our series. 

Facades in the Real World 

Facades are all around us in the real world.  Operating systems are one such example - you don't see all the inner workings of your computer, but the OS provides a simplified interface to use the machine. Buildings also have a facade - the exterior of the building. Wikipedia gives us a nice link between software architecture and standard architecture: 

In architecture, the facade of a building is often the most important from a design standpoint, as it sets the tone for the rest of the building

So, in a nutshell, a Facade aims to make things look cleaner and more appealling.

The Facade Pattern

Like the Adapter pattern, Facade is known as a  structural pattern, as it's used to identifying a simple way to realize relationships between entities. The definition of Facade provided in the original Gang of Four book on Design Patterns states: 

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

The diagram definition of the Facade pattern is quite simple - all you're really doing is insulating client from the subsystem:

 

Like the adapter pattern, the Facade can be used to hide the inner workings of a third party library, or some legacy code.  All that the client needs to do is interact with the Facade, and not the subsystem that it is encompassing.

The following sequence diagram illustrates how the pattern is used by a client: 

 

Where Would I Use This Pattern?

As the concept behind facade is to simplify an interface, service oriented architectures make use of the facade pattern. For example, in web services, one web service might provide access to a number of smaller services that have been hidden from the caller by the facade. Similarly, a typical pattern in OSGi bundles is to provide an interface package that is exposed to users of the bundle. All other packages are hidden from the user.

So How Does It Work In Java?

Let's put together a simple example in Java code to illustrate the pattern. Let's take a travel agent site for example, that allows you to book hotels and flights. We have a HotelBooker:

01. public class HotelBooker
02. {
03.  
04. public ArrayList<Hotel> getHotelNamesFor(Date from, Date to)
05. {
06. //returns hotels available in the particular date range
07.  
08. }
09.  
10. }

And a FlightBooker:

01. public class FlightBooker
02. {
03.  
04. public ArrayList<Flight> getFlightsFor(Date from, Date to)
05. {
06. //returns flights available in the particular date range
07.  
08. }
09.  
10. }

Both of these have Hotel and Flight datatypes, which the client has knowledge about. They could be provided in the same package as the Facade for example. 

The TravelFacade class allows the user to get their Hotel and Flight information in one call:

 

01. public class TravelFacade
02. {
03.  
04. private HotelBooker hotelBooker;
05. private FlightBooker flightBooker;
06.  
07. public void getFlightsAndHotels(Date from, Data to)
08. {
09. ArrayList<Flight> flights = flightBooker.getFlightsFor(from, to);
10. ArrayList<Hotel> hotels = hotelBooker.getHotelsFor(from, to);
11.  
12. //process and return
13.  
14. }
15.  
16. }

All that the client needs to worry about is the Facade class: 

01. public class Client
02. {
03.  
04. public static void main(String[] args)
05. {
06. TravelFacade facade = new TravelFacade();
07. facade.getFlightsAndHotels(from, to);
08. }
09. }

As you can see, it's just a simple approach to encapsulating data.

Watch Out for the Downsides

By introducing the Facade into your code, you will be hardwiring subsystems into the Facade. This is fine if the subsystem never changes, but if it does, your Facade could be broken. Therefore, developers working on the subsystem should be made aware of any Facade around their code.

Next Up

 We'll get to the Singleton pattern next week.

Tags: 

Comments

Mateusz Mrozewski replied on Sat, 2010/02/13 - 5:23am

In the second listing the class name should be FlightBooker.

 Thank you for the series, it's excellent. It will be very valuable for anyone who wants to learn design patterns. Keep going :)

Vedhas Pitkar replied on Sun, 2010/02/14 - 11:54pm

Indeed, very nice series!! I like your "in the Real World " part more as it helps one to visualize the design pattern. Awaiting the next one.. PS: I would be nice if you would also explain the subtle differences between 2 similar patterns i.e. Adapter & Facade, Strategy & template etc...

James Sugrue replied on Mon, 2010/02/15 - 2:46am in response to: Vedhas Pitkar

Thanks for the comments. I'll make sure to add in the differences in the future patterns, and will edit the existing articles in the series soon to illustrate the differences.

 

Sriram Varadharajan replied on Mon, 2010/02/15 - 8:48am

excellent work . looking forward to singleton pattern discussion .

Bogdan Marian replied on Mon, 2010/02/15 - 9:06am

I really appreciate your effort  you put in describing the GOF design patterns.

I can hardly wait for the next articles.

Well done, James.

prashant jalasutram replied on Tue, 2010/02/16 - 5:53am

nice post sir. Keep writing about all patterns. Thanks Prashant Jalasutram http://prasanthaboutjava.blogspot.com/

Mladen Girazovski replied on Tue, 2010/02/16 - 2:38pm in response to: Sriram Varadharajan

looking forward to singleton pattern discussion . 

 You're kidding, right?

Sudhakar Ramasamy replied on Wed, 2010/03/17 - 10:48pm

The TravelFacade.getFlightsAndHotels(Date from, Date to) should return an object, correct?

JavaOnZone Neon replied on Thu, 2011/07/21 - 2:40pm

Like it. It's easy to understand with these simple examples.

nilesh gule replied on Mon, 2012/07/16 - 9:59am

Nicely depicted using hotels and flights. This is one of the best example I have seen for Facade pattern. I recently posted about C# implementation of Facade using Home Loan as an example. 

http://www.nileshgule.com/2012/07/facade-design-pattern.html

Reference: http://java.dzone.com/articles/design-patterns-uncovered-1

内容概要:本文围绕“基于改进滑模控制的永磁同步电机调速系统模型研究”展开,重点介绍在Simulink环境中构建和仿真永磁同步电机(PMSM)调速系统的方法,采用改进滑模控制策略以提升系统鲁棒性与动态性能。文中系统阐述了控制算法的设计原理、系统建模流程、关键模块搭建及仿真结果分析,旨在复现高水平科研成果(SCI/EI级别),并通过仿真实验验证所提控制策略的有效性。该研究属于电机控制与电力电子领域的前沿方向,对高精度伺服系统、新能源汽车电驱动系统等实际应用场景具有重要的理论指导和工程参考价值; 适合人群:具备自动控制理论基础和Simulink/MATLAB仿真能力,从事电气工程、自动化、电力电子等相关专业的研究生、科研人员及工程技术人员,尤其适合致力于复现高水平学术论文成果的研究者; 使用场景及目标:①深入学习永磁同步电机矢量控制与滑模变结构控制的核心原理与建模方法;②复现并理解SCI/EI期刊中先进电机控制算法的技术细节;③开展电机控制系统仿真研究,优化控制参数,提升系统抗干扰能力、稳态精度与动态响应性能; 阅读建议:建议结合文中提及的完整资源包(含Simulink模型、MATLAB代码、详细说明文档)进行实践操作,重点关注控制策略的实现逻辑与仿真调试过程,注重理论推导与仿真实验相结合,同时参考同类高水平研究以拓展技术视野。
内容概要:本文提出了一种基于数据驱动的Koopman算子与递归神经网络(RNN)相结合的模型线性化方法,旨在解决纳米定位系统中因强非线性、迟滞和蠕变效应导致的建模困难问题。该方法通过Koopman算子将非线性动态系统映射至高维线性空间,利用RNN学习系统的时间序列演化特征,从而实现对复杂动态行为的精确建模与预测,并进一步集成于模型预测控制(MPC)框架中,显著提升了纳米定位系统的控制精度、动态响应能力与运行稳定性。整个算法体系在Matlab平台上完成代码实现与仿真实验验证,展示了良好的控制性能与工程应用潜力。; 适合人群:具备控制理论、非线性系统建模、机器学习及智能控制基础,从事精密仪器控制、高端制造装备研发、自动化系统设计等领域的研究生、科研人员及工程技术开发者。; 使用场景及目标:①应对扫描探针显微镜、光刻机、超精密加工平台等纳米级定位设备中的非线性建模挑战;②提升高精度运动系统的实时预测控制性能,抑制迟滞与蠕变带来的定位误差;③为数据驱动的非线性系统线性化与先进控制策略(如MPC)的融合提供可复现、可扩展的技术范例。; 阅读建议:建议读者结合提供的Matlab代码,深入理解Koopman观测矩阵构造、RNN网络训练流程及MPC控制器设计之间的协同机制,重点关注数据预处理、特征提取、模型训练与闭环控制仿真的完整链路,以便在相似高精度控制系统中进行迁移与优化应用。
内容概要:本文系统研究了基于动态三维环境下的Q-Learning算法在无人机自主避障路径规划中的应用,旨在通过强化学习实现无人机在复杂、动态空间中的智能决策与安全飞行。研究构建了完整的Q-Learning模型框架,涵盖状态空间定义、动作策略设计与奖励函数构建,重点提升了算法在存在移动障碍物场景下的路径规划能力与实时避障性能。通过Matlab仿真平台实现了算法的全流程建模与验证,展示了其在路径最优性、环境适应性与运行稳定性方面的优势,并为后续多机协同、城市密集环境等高级应用场景提供了可扩展的技术基础与代码支持。; 适合人群:具备一定编程基础和控制理论知识,从事无人机导航、智能优化算法或强化学习相关研究的科研人员及研究生。; 使用场景及目标:① 掌握Q-Learning算法在三维动态路径规划中的建模与实现方法;② 学习如何将强化学习技术应用于实际工程问题如无人机自主避障;③ 为深入研究多智能体协同、复杂非结构化环境下的路径规划提供算法原型与仿真基础; 阅读建议:建议读者结合提供的Matlab代码进行仿真实验,深入理解状态表示与奖励机制的设计逻辑,尝试调整算法参数或引入新的动态障碍物模式以评估鲁棒性,并可进一步对比其他智能算法(如DQN、A*、DWA等)在相同环境下的性能差异。
源码下载地址: https://pan.quark.cn/s/a4b39357ea24 微信小程序商城 微信小程序商城,微信小程序微店,长期维护版本,欢迎大家踊跃提交贡献代码; 使用说明和常见问题,可参阅下面的说明,如还有疑问,可访问工厂官网 https://www.it120.cc/ 寻求帮助! 新增直播带货支持,具体详见使用说明 今日头条/抖音小程序版本 本项目的今日头条/抖音小程序版本,请移步至下面的地址: https://.com/EastWorld/tt-app-mall 扫码体验 详细配置/使用教程 https://www.it120.cc/help/ikfe2k.html 遇到使用问题? 点击这里找答案,可用关键词搜索 其他优秀开源模板推荐 天使童装 / 码云镜像 / GitCode镜像 天使童装(uni-app版本) / 码云镜像 / GitCode镜像 简约精品商城(uni-app版本) / 码云镜像 / GitCode镜像 舔果果小铺(升级版) 面馆风格小程序 AI名片 / 码云镜像 / GitCode镜像 仿海底捞订座排队 (uni-app) / 码云镜像 / GitCode镜像 H5版本商城/餐饮 / 码云镜像 / GitCode镜像 餐饮点餐 / 码云镜像 / GitCode镜像 企业微展 / 码云镜像 / GitCode镜像 无人棋牌室 / 码云镜像 / GitCode镜像 酒店客房服务小程序 / 码云镜像 / GitCode镜像 面包店风格小程序 / 码云镜像 / GitCode镜像 朋友圈发圈素材小程序 / 码云镜像 / GitCode镜像 小红书企业微展 / 码云镜像 / GitCode镜像 旧物回收、废品回收 / 码云镜像 / ...
源码下载地址: https://pan.quark.cn/s/a4b39357ea24 在电子数据通信领域中,串口通信光耦隔离电路是一种被广泛应用的电路设计方案。该方案借助光耦合器(optocoupler)达成电路的电气隔离,进而保障通信的稳定性和安全性。在此之后,我们将详细研究串口通信中的光耦隔离技术、电路构造,以及与波特率和误码率之间的相互联系。光耦合器是一种通过光信号传递电信号的半导体装置,它一般包含一个发光二极管(LED)和一个光敏三极管或其他类型的光敏单元。当LED受到电信号驱动时,它会发出光,该光信号随后被光敏元件捕获并转化为电信号,由此实现电平的隔离。在串口通信电路构造中,光耦合器的主要功能是将微处理器等发送部分与接收部分分隔开来。这种隔离措施能够有效防止两部分电路之间的电气干扰,并在一定程度上增强系统的抗干扰性能。比如,当发送端设备遭遇雷击或其他高压冲击时,光耦隔离能够使接收端设备免于受损。光耦隔离电路通常应用于RS232、RS485等串行通信接口,目的是确保信号在传输期间不受电势差、电流、噪声等外部因素的不良影响。在采用光耦隔离技术时,必须特别关注信号的速率,即波特率。波特率是衡量串口通信中信号传输速度的单位,它表示每秒钟能够传输的信号元素(如位)的多少。在构建光耦隔离电路时,必须将光耦合器的传输速率纳入考量。由于光耦合器的响应周期和传输延迟,采用光耦合器的隔离电路或许无法应对过高的波特率。高波特率代表着更高的信号频率,这可能会导致光耦合器无法及时准确地解析信号,从而造成误码率增加,影响数据传输的精确度。因此,在构建串口通信光耦隔离电路时,应审慎挑选合适的光耦合器和电路构造,以确保在可接受的误码率范围内进行数据通信。在选择光耦合器时,应参照其最...
内容概要:本文系统阐述了频域视角下的风险溢出网络研究,重点聚焦从Diebold-Yilmaz(DY)溢出指数到Baruník-Křehlík(BK)溢出指数的理论演进与实证实现。BK方法通过傅里叶变换将风险溢出效应分解至不同频率成分,从而能够精细识别金融市场间短期冲击与长期趋势的风险传导机制,显著提升了对系统性金融风险动态结构的理解能力。文中配套提供了完整的Matlab代码实现流程与实际案例分析,涵盖谱密度矩阵估计、广义方差分解及频域权重计算等关键步骤,帮助读者掌握从数据处理到结果可视化与经济解释的全过程。; 适合人群:具备扎实计量经济学基础和良好Matlab编程能力的高校研究生、博士生及金融领域科研人员,特别适用于从事金融风险管理、资产定价、宏观经济与金融市场联动性研究的学者,以及希望将前沿量化工具应用于实证分析的金融从业者。; 使用场景及目标:①用于学术研究中构建高频与低频风险溢出网络,深入剖析不同投资周期下市场间的传染路径与主导关系;②辅助监管机构和政策制定者识别系统性风险的源头、传播渠道与时变特征,提升宏观审慎监管的精准性与时效性;③作为高级金融计量学或实证资产定价课程的教学案例,培养学生动手实现并解读复杂风险测度工具的能力。; 阅读建议:建议读者结合文中提供的Matlab代码逐行调试与运行,深入理解频域分析中谱密度、广义方差分解及频域权重的核心算法逻辑,并尝试将其应用于自身的研究课题或实际数据。同时,强烈推荐阅读Baruník & Křehlík(2018)等原始文献,以夯实理论基础,全面把握方法的假设前提与适用边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值