Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>...most sites point out that the mediator “adds functionality”...</em></p> </blockquote> <p>The <strong>facade</strong> only exposes the existing functionality from a different perspective.</p> <p>The <strong>mediator</strong> "adds" functionality because it combines different existing functionality to create a new one.</p> <p>Take the following example:</p> <p>You have a logging system. From that logging system you can either log to a file, to a socket, or to a database.</p> <p>Using the facade design pattern you would "hide" all the relationships from existing functionality behind a single "interface" the one that the facade exposes.</p> <p><em>Client code:</em></p> <pre><code> Logger logger = new Logger(); logger.initLogger("someLogger"); logger.debug("message"); </code></pre> <p>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:</p> <p><em>Implementation:</em></p> <pre><code> 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 ); } } </code></pre> <p>The functionality already exist. 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.</p> <p>In the other hand the mediator add the new functionality by combining different objects. </p> <p><em>Same Client code:</em></p> <pre><code> Logger logger = new Logger(); logger.initLogger("someLogger"); logger.debug("message"); </code></pre> <p><em>Implementation:</em></p> <pre><code> 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(); } } } </code></pre> <p>In this code, the mediator is the one that contains the business logic to create the appropriate "channel" to log and also to make the log into that channel. The mediator is "creating" the functionality.</p> <p>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.</p> <p>Finally, the <strong>facade</strong> is an structural pattern, that is it describes the composition of the objects, while the <strong>mediator</strong> is an behavioral, that is , it describes the way the objects interact.</p> <p>I hope this helps. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload