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