Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

design patterns - Façade vs. Mediator

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

...most sites point out that the mediator “adds functionality”...

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:

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

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:

 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 );
      }     
 }

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:

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

I hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...