Facade against the intermediary

I explored the difference between the two patterns.

I understand that a facade encapsulates access to a subsystem, and a mediator encapsulates interactions between components.

I understand that the components of the auxiliary system do not know about the facade, where, since the components, obviously, know about the intermediary.

I am currently using a facade to encapsulate a way to retrieve configuration information, for example. App.Config, user settings stored in SQL, assembly information, etc., And an intermediary for navigating between different forms of windows.

However, most sites indicate that the mediator "adds functionality." What do they mean by that? How does a mediator add functionality?

+65
design-patterns facade mediator
Jan 27 '09 at 0:52
source share
7 answers

... most sites indicate that the mediator "adds functionality" ...

the facade provides only existing functions from a different perspective.

The broker “adds” functionality because it combines the various existing functions to create a new one.

Take the following example:

You have a logging system. From this registration system, you can either enter the file, or the socket, or the database.

Using the facade design template, you will "hide" all the relationships from the existing functionality behind a single "interface" that provides the facade.

Client Code:

Logger logger = new Logger(); logger.initLogger("someLogger"); logger.debug("message"); 

An implementation may include the interaction of many objects. But in the end, functionality already exists. The debugging method is probably implemented as follows:

Implementation:

  class Logger { private LoggerImpl internalLogger; private LoggerManager manager; public void initLogger( String loggerName ) { this.internalLogger = manager.getLogger( loggerName ); } public void debug( String message ) { this.internalLogger.debug( message ); } } 

Functionality already exists. The facade only hides it. In this hypothetical case, LoggerManager handles the creation of the correct registrar, and LoggerImpl is a closed package object that has a "debugging" method. Thus, Facade does not add functionality that it simply delegates to some existing objects.

On the other hand, the mediator adds new functionality by combining different objects.

Same customer code:

  Logger logger = new Logger(); logger.initLogger("someLogger"); logger.debug("message"); 

Implementation:

  class Logger { private java.io.PrintStream out; private java.net.Socket client; private java.sql.Connection dbConnection; private String loggerName; public void initLogger( String loggerName ) { this.loggerName = loggerName; if ( loggerName == "someLogger" ) { out = new PrintStream( new File("app.log")); } else if ( loggerName == "serverLog" ) { client = new Socket("127.0.0.1", 1234 ); } else if( loggerName == "dblog") { dbConnection = Class.forName()... . } } public void debug( String message ) { if ( loggerName == "someLogger" ) { out.println( message ); } else if ( loggerName == "serverLog" ) { ObjectOutputStrewam oos = new ObjectOutputStrewam( client.getOutputStream()); oos.writeObject( message ); } else if( loggerName == "dblog") { Pstmt pstmt = dbConnection.prepareStatment( LOG_SQL ); pstmt.setParameter(1, message ); pstmt.executeUpdate(); dbConnection.commit(); } } } 

In this code, an intermediary is one that contains business logic for creating the corresponding “channel” for registration, as well as for recording in this channel. It "creates" functionality.

Of course, there are more efficient ways to implement this using polymorphism, but here you need to show how the mediator “adds” new functionality by combining existing functionality (in my example, it wasn’t too bad), but imagine a mediator reading a remote host from the database, where the log is written, then creates a client, and finally writes a log message to this print stream client. Thus, the intermediary will be "indirectly" between different objects.

Finally, the facade is a structural template, that is, it describes the composition of objects, and the mediator is a behavioral one, that is, it describes the way objects interact.

Hope this helps.

+84
Jan 27 '09 at 4:53
source share

I use a pick to add the functionality of a log file.

It works as follows:

  • Obj A tells the intermediary that he needs to do something.
  • The mediator sends a message to various client sites.
  • Obj B performs the Obj A task and sends the appropriate message through an intermediary.
  • Meanwhile, Obj C also sends both messages to the broker and logs the results. This way we can get user statistics from log files.
  • Obj D can also be a means of checking errors, so if Obj B replies that Obj Request is not possible, Obj D may be what informs the user about it. Errors can now be logged in a different file than normal activity, and can use some other ways to behave (sound, whatever) that Obj A should not really touch itself.
+12
Jan 27 '09 at 1:03
source share

under the appropriate patterns, gof says: Facade (185) differs from Mediator in that it abstracts the subsystem of objects to provide a more convenient interface. Its protocol is unidirectional; that is, Facade objects handle subsystem class requests, and not vice versa. On the contrary, the Mediator allows collaborative behavior that the colleagues' objects do not provide or cannot provide, and the protocol is multidirectional.

+9
Jun 13 '15 at 7:25
source share

A simple analogy:

Facade: as a parking lot, on call

 parkingLot.Out(car1); 

mab - a simple chain works:

 { car1.StartEngin(); attendant.charge(); car1.driverOut(); } 

Mediator: like a traffic light.

There are interactions between the light and the car,

and cars are controlled by this condition.

I may be this broker "adds functionality




And about the definition:

Facade Type: Structural

Mediator Type: Behavioral

the facade, more concerned with components , contained in a unified interface

and the pick refers to how a set of objects interacts .

+5
May 31 '13 at
source share

I thought the difference was directional: a facade is a one-way connection between a client and a facade; an intermediary can be a two-way conversation, with messages flowing between the client and the intermediary.

+3
Jan 27 '09 at 2:07
source share

From the book “Samples of Figures”, the KEY for the intermediary template is described as follows: “It (the intermediary) acts as a communication hub for widgets (ie,“ Groups ”of interdependent objects).

In other words, the intermediary object is the only superobject that knows all the other objects in the group of interacting objects and how they should interact with each other. All other objects should interact with the intermediary object, and not with each other.

In contrast, a facade is a “unified interface” for a set of interfaces in a subsystem — for use by a subsystem by consumers — not among the components of the subsystem.

+2
Jan 12 2018-11-11T00:
source share

Information on the facade template can be found in this SE question:

What is a facade design template?

Facade provides a simple and unified interface for a complex system.

A real world example ( clearart + hotel reservation ) is available in this post:

What is a facade design template?

Mediator pattern: Define an object that encapsulates how a set of objects interacts. The mediator promotes a free connection, preventing direct access of objects to each other, and allows you to independently change their interaction.

An example of a real mesh network topology in the real world is given below in the SE question:

Object Oriented Vs Observer Plectrum Design Patterns

With respect to your request for Mediator, liability is added:

  • The facade provides only an interface to existing subsystems . Existing subsystems are not aware of the Facade class itself.

  • The intermediary knows about the objects of colleagues . This allows you to communicate between different colleagues. In the example I cited in a related question, ConcreteMediator (NetworkMediator) sends notifications of registration and unregistration of one colleague to all other colleagues.

0
Aug 08 '16 at 16:09 on
source share