设计模式-了解立面模式 [英] Design Patterns - Understanding Facade Pattern

查看:175
本文介绍了设计模式-了解立面模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是设计模式的新手,正在尝试学习它们的典型外观。现在,我正在尝试了解外观模式。我觉得Facade模式是一个相当广泛的概念,所以我想知道我的第二个图表是否会被视为Facade Template的一部分。



我知道一个典型的例子外观模式基本上看起来像这样(以A级为外观):





但是如果我们有一个更细微的图表,怎么办:





A类是否仍被视为外观类,还是取决于上下文?

解决方案

首先,要了解外观,我喜欢将其视为一种重构。想象一下您的两个图没有的门面类。客户必须直接与Facade管理的所有类进行交互。这样,它们将更复杂,耦合更多。



门面为客户端提供简化的服务(减少耦合),隐藏了门面背后的复杂性。



我最喜欢的示例(在Java中)是 JOptionPane 类。它在Java的最早版本中不存在,如果您想创建是/否问题对话框,您(作为客户端)必须管理对Dialog,Button等的所有调用,并处理事件,等等。所有这些复杂性已简化为 JOptionPane 门面类中的静态方法。这是



现在您的问题:


是否仍将A级视为外观类还是取决于上下文?


如果 A 是为客户提供简化的服务,以有效使用 B C 的复杂子系统F E ,没有它们,客户端将不得不直接与所有它们进行交互(耦合),然后我说 A 是一个门面。


I'm new to Design Patterns and am trying to learn how they typically look like. Right now I'm trying to understand the Facade Pattern. I feel like the Facade Pattern is a rather broad concept, and so I was wondering if my second diagram would be considered a part of the Facade Template.

I know a typical Facade Pattern basically looks like this(with the A-class being the facade):

But what if we have a more subtle diagram like this:

Would the A-class still be considered a Facade-class or does it depend on the context?

解决方案

First, to understand facade, I like to think of it as a kind of refactoring. Imagine both your diagrams without the facade classes. The clients would have to interact directly with all the classes that the facade manages. As such, they would be more complex, with more coupling.

A facade provides a simplified service to the clients (reducing coupling), hiding the complexity behind the facade.

My favorite example (in Java) is the JOptionPane class. It did not exist in the earliest versions of Java, and if you wanted to create a Yes/No question dialog, you (as a client) had to manage all the calls to Dialog, Button, etc. as well as handle the events, etc. All that complexity has been simplified into a static method inside the JOptionPane facade class. Here's a UML diagram from https://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/facade.html

Now to your question:

Would the A-class still be considered a Facade-class or does it depend on the context?

If A is providing a simplified service to the clients that effectively uses the complex subsystem of B, C, F and E, without which the clients would have to interact with (be coupled to) all of them directly, then I'd say A is a facade.

这篇关于设计模式-了解立面模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆