Difference between Facade and Mediator Design Pattern?

What is the difference between the Facade Design Pattern and the Mediator Design Pattern. I want to understand which design pattern to choose between these two scenarios. I went through the following links and found both in terms of usage.

Facade design scheme: http://www.tutorialspoint.com/design_pattern/facade_pattern.htm

Mediator design diagram: http://www.java2s.com/Tutorial/Java/0460__Design-Pattern/CoordinatingYourObjectswiththeMediatorPatterns.htm

I have a confusion about the following code segment that is similar to both design patterns.

Facade class:

public class ShapeMaker {
   private Shape circle;
   private Shape rectangle;
   private Shape square;

   public ShapeMaker() {
      circle = new Circle();
      rectangle = new Rectangle();
      square = new Square();
   }

   public void drawCircle(){
     circle.draw();
   }
   public void drawRectangle(){
     rectangle.draw();
   }
   public void drawSquare(){
     square.draw();
   }
}

      

Mediator class:

   public class Mediator {
         Welcome welcome;
         Browse browse;
         Purchase purchase;
         Exit exit;

        public Mediator() {
          welcome = new Welcome(this);
          browse = new Browse(this);
          purchase = new Purchase(this);
          exit = new Exit(this);
       }

      public void handle(String state) {
          if (state.equals("welcome.shop")) {
            browse.execute();
      } else if (state.equals("shop.purchase")) {
            purchase.execute();
      } else if (state.equals("purchase.exit")) {
             exit.execute();
      }

      

+3


source to share


4 answers


I have a confusion about the following code segment that is similar to both design patterns.

I think you can see the composite aspects of both patterns.

Facade links to various existing subsystem classes to add some typical functionality that makes the subsystem easier to use. The following code example ShapeMaker

provides services that make it easier to create shapes.

Mediator links to different colleagues who need to collaborate to minimize colleagues' knowledge of each other. Minimizing knowledge has the side effect of reducing communication between colleagues (they only know the facilitator) and increasing their cohesion (they tend to have less anxiety since they don't know the bigger picture).



In both patterns, the centralized class takes responsibility for the complexity of the classes it is associated with.

Here are the basic patterns in UML from Gang of Four:

Facade pattern

Mediator pattern

0


source


The facade provides existing functionality, and the pick adds to existing functionality.

If you look at the example facade, you can see that you are not adding any new functionality, just giving the current objects a new perspective. For example, Circle already exists, and you simply abstract away from the circle using the drawCircle method.



If you look at your mediator class, you can see that the method handle()

provides additional functionality by checking the state. If you selected conditions, you will have a facade template, since there will be no additional functionality.

+5


source


facade gives you a simple interface that interacts with a set of consistent classes. For example, a remote control for your home that controls all kinds of equipment in your home would be the facade. You simply interact with the remote control, and the remote control determines which device should respond and which signal to send.

the mediator pattern takes over the relationship between two objects, without the two objects it is necessary to reference each other directly. A real life example is sending a letter, you send a letter, and the postal service picks it up and makes sure it gets to the recipient. Without telling them which path they should take. This is an intermediary.

However, your examples are more like creation pattern (looks like a factory) and behavioral pattern (state pattern). I understand your confusion.

+2


source


Facade notes on the template template:

  • A simple interface is required to access a complex system.
  • Subsystem abstractions and implementations are closely related.
  • An entry point must be specified for each layer of the layered software.
  • The system is very complex or difficult to understand.

Related posts:

What is a facade design template?

Mediator notes with template (article about zona):

  • Mediator schema is useful when the communication logic between objects is complex, we can have a central communication point that takes care of the communication logic.

  • We should not use the mediator pattern just to achieve a loss of connection, because if the number of mediators grows, then it will be difficult to maintain them.

Composition:

enter image description here

Mediator

defines an interface for communication between objects Colleague

.

ConcreteMediator

implements the interface Mediator

and coordinates communication between objects Colleague

.

He knows everything Colleagues

and their purpose in relation to interaction. ConcreteColleague

communicates with other colleagues through Mediator

.

At your request:

  • Your first example presents a template Facade

    . ShapeMaker

    is the entry point into a complex system consisting of various subsystems of systems Shape

    .

  • The second example does not reflect the pattern Mediator

    in the true sense. The interactions were hardcoded. You must define interfaces and programs for these interfaces. See the above article dzone

    for a better understanding of the example Mediator

    .

+1


source







All Articles