Façade 与 Mediator
我一直在研究这两种模式之间的区别。
据我所知,门面封装了对子系统的访问,中介器封装了组件之间的交互。
我知道子系统组件不知道外观,而组件显然知道中介。
我目前正在使用外观来封装检索配置信息的方法,例如 App.Config、存储在 SQL 中的用户设置、程序集信息等,以及用于在不同窗口窗体之间导航的中介器。
然而,大多数网站指出调解器“增加了功能”。 他们这是什么意思? 中介者如何添加功能?
I've been researching the difference between these two patterns.
I understand that facade encapsulates access to a sub system and mediator encapsulates the interactions between components.
I understand that sub system components are not aware of the facade, where as components are obviously aware of the mediator.
I'm currently using a facade for encapsulating the method of retrieving configuration information, e.g. App.Config, user setting stored in SQL, Assembly info, etc, and a mediator for navigation between different windows forms.
However, most sites point out that the mediator “adds functionality”. What do they mean by this? How does mediator add functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从《设计模式》一书中,Mediator模式的KEY描述如下:
“它(中介者)充当小部件(即“一组”相互依赖的对象)的通信中心。”
换句话说,中介对象是唯一的超级对象,它知道一组协作对象中的所有其他对象以及它们应该如何彼此交互。 所有其他对象都应该与中介对象交互,而不是彼此交互。
相反,外观是子系统中一组接口的“统一接口”——供子系统的使用者使用——而不是子系统的组件之间。
From the "Design Patterns" book, the KEY of the Mediator pattern is described as follows:
"It (a mediator) acts as a HUB of communication for widgets (i.e., 'a' group of interdependent objects)."
In other words, a mediator object is the sole superobject that knows all other objects in a group of collaborating objects and how they should interact with each other. All other objects should interact with the mediator object, instead of each other.
In contrast, a facade is a "unified interface" for a set of interfaces in a subsystem - for use by consumers of the subsystem - not among the components of the subsystem.
您可以在此 SE 问题中找到有关外观模式的详细信息:
什么是外观设计模式?
Facade
为复杂的系统提供了一个简单而统一的界面。真实世界的示例(cleartrip 航班+酒店预订)可在这篇文章中找到:
什么是外观设计模式?
Mediator 模式:定义一个对象,封装了一组对象如何交互。 中介器通过防止对象显式地相互引用来促进松散耦合,并且它允许您独立地改变它们的交互。
下面的 SE 问题提供了网格网络拓扑的真实示例:
Mediator 与 Observer 面向对象设计模式
关于您对Mediator的查询增加了责任:
Facade仅提供现有子系统的接口。 现有的子系统不知道 Facade 类本身。
中介者了解同事对象。 它使不同同事之间能够进行沟通。 在我在链接问题中引用的示例中,ConcreteMediator(NetworkMediator) 向所有其他同事发送一位同事的注册和取消注册事件的通知。
You can find details about Facade pattern in this SE question:
What is Facade Design Pattern?
Facade
provides a simple and unified interface to complex system.Real world example (cleartrip flight+hotel booking) is available in this post:
What is Facade Design Pattern?
Mediator pattern: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
A real world example of Mesh network topology has been provided in below SE question:
Mediator Vs Observer Object-Oriented Design Patterns
Regarding your query on Mediator adds responsibility:
Facade provides only interface to existing sub-systems. Existing sub-systems are not aware of Facade class itself.
Mediator knows about colleague objects. It enables communication between different colleagues. In the example I have quoted in linked question, ConcreteMediator(NetworkMediator) sends notifications of register and unregister event of one colleague to all other colleagues.
两者都对另一组对象施加某种策略。 Facade 从上面强制执行策略,Mediator 从下面强制执行策略。 Facade 的使用是可见的、具有约束性的,而Mediator 的使用是不可见的、具有赋能性的。
当您想要为一组具有复杂且通用接口的对象提供简单且特定的接口时,可以使用外观模式。
中介者模式还强加策略。 然而,Facade 以可见且受约束的方式施加其策略,而 Mediator 以隐藏且不受约束的方式施加其策略。
敏捷软件开发、原则、模式和实践 Robert C. Martin。
Both impose some kind of policy on another group of objects. Facade imposes the policy from above, and Mediator imposes the policy from below. The use of Facade is visible and constraining, while the use of Mediator is invisible and enabling.
The Facade pattern is used when you want to provide a simple and specific interface to a group of objects that has a complex and general interface.
The Mediator pattern also imposes policy. However, whereas Facade imposed its policy in a visible and constraining way, Mediator imposes its policies in a hidden and unconstrained way.
Agile Software Development, Principles, Patterns, and Practices Robert C. Martin.
外观仅从不同的角度公开现有功能。
中介“添加”功能,因为它结合了不同的现有功能来创建新功能。
举个例子:
你有一个日志系统。 从该日志系统,您可以记录到文件、套接字或数据库。
使用外观设计模式,您可以将现有功能的所有关系“隐藏”在外观所公开的单个“界面”后面。
客户端代码:
实现可能涉及许多对象的交互。 但最终,该功能已经存在。 可能“调试”方法的实现如下:
实现:
该功能已经存在。 外观只是隐藏了它。 在这个假设的情况下,LoggerManager 处理正确记录器的创建,并且 LoggerImpl 是一个具有“debug”方法的包私有对象。 这样,Facade 就不会添加功能,它只是将其委托给某些现有对象。
另一方面,中介者通过组合不同的对象来添加新的功能。
相同的客户端代码:
实现:
在此代码中,中介者包含业务逻辑,用于创建适当的“通道”来记录日志并将日志记录到其中那个频道。 中介者正在“创建”功能。
当然,有更好的方法可以使用多态性来实现这一点,但这里的重点是展示中介者如何通过组合现有功能来“添加”新功能(在我的示例中没有表现出非常抱歉),但是想象一下中介者,请阅读从远程主机的数据库中记录日志,然后创建一个客户端,最后将日志消息写入该客户端打印流。 这样,中介者就可以在不同的对象之间进行“中介”。
最后,外观是一种结构模式,即它描述了对象的组成,而中介是一种行为,即它描述了对象交互的方式。
The facade only exposes the existing functionality from a different perspective.
The mediator "adds" functionality because it combines different existing functionality to create a new one.
Take the following example:
You have a logging system. From that logging system, you can either log to a file, to a socket, or to a database.
Using the facade design pattern you would "hide" all the relationships from existing functionality behind a single "interface" the one that the facade exposes.
Client code:
The implementation may involve the interaction of many objects. But at the end, the functionality already exists. Probably the "debug" method is implemented as follows:
Implementation:
The functionality already exists. The facade only hides it. In this hypothetical case, the LoggerManager handles the creation of the correct logger, and the LoggerImpl is a package-private object that has the "debug" method. This way the Facade is not adding functionality it is just delegating to some existing objects.
On the other hand the mediator adds new functionality by combining different objects.
Same Client code:
Implementation:
In this code, the mediator is the one that contains the business logic to create an appropriate "channel" to log and also to make the log into that channel. The mediator is "creating" the functionality.
Of course, there are better ways to implement this using polymorphism, but the point here is to show how the mediator "adds" new functionality by combining existing functionality ( in my sample didn't show very much sorry ) but imagine the mediator, read from the database the remote host where to log, then creates a client and finally write to that client print stream the log message. This way the mediator would be "mediating" between the different objects.
Finally, the facade is a structural pattern, that is it describes the composition of the objects, while the mediator is a behavioral, that is, it describes the way the objects interact.
在相关模式下,gof 说:Facade (185) 与 Mediator 不同,它抽象了对象子系统以提供更多方便的界面。 其协议是单向的; 也就是说,Facade 对象向子系统类发出请求,但反之则不然。 相比之下,Mediator 支持同事对象不提供或不能提供的协作行为,并且该协议是多向的。
under related patterns, gof says: Facade (185) differs from Mediator in that it abstracts a subsystem of objects to provide a more convenient interface. Its protocol is unidirectional; that is, Facade objects make requests of the subsystem classes but not vice versa. In contrast, Mediator enables cooperative behavior that colleague objects don't or can't provide, and the protocol is multidirectional.
我正在使用调解器添加日志文件功能。
它的工作原理如下:
I'm using mediator to add log file functionality.
It works like this:
打个简单的比方:
Facade:就像一个停车场,当调用的时候
可能是一个简单的链条的工作原理:
Mediator:就像红绿灯。
光和汽车之间存在交互,
汽车由其状态控制。
我想这可能是中介“添加功能”
关于定义:
外观类型:结构
中介类型:行为
外观更关心组件包含在统一界面中,
中介者关注一组对象如何交互强>。
Take a simple analogy:
Facade: like a parking lot, when call
mab be a simple chain works:
Mediator: like traffic light.
There are interactions between light and car,
and cars are controlled by it's state.
I though maybe this is the mediator “adds functionality”
And about the definition:
Facade's Type: Structural
Mediator's Type: Behavioral
facade more concerned about the components were contained in the unified interface,
and mediator concern how a set of objects interact.
我认为这种区别是方向性的:门面是客户端和门面之间的单向通信; 中介者可以是双向对话,消息在客户端和中介者之间来回流动。
I thought the distinction was directional: facade is a one-way communication between client and facade; mediator can be a two-way conversation, with messages flowing back and forth between the client and mediator.