Façade vs. Mediator

2019-01-16 03:35发布

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?

8条回答
等我变得足够好
2楼-- · 2019-01-16 04:04

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.

查看更多
三岁会撩人
3楼-- · 2019-01-16 04:07

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:

  1. Facade provides only interface to existing sub-systems. Existing sub-systems are not aware of Facade class itself.

  2. 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.

查看更多
登录 后发表回答