简体   繁体   中英

Difference between Facade and Mediator Design pattern?

What is the difference between facade and mediator design pattern. I want understand which design pattern to choose between these two in which scenario. I was going through the following links and found both same in terms of use case.

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

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

I have confusion in following code segment which looks similar in both the 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();
      }

The facade exposes existing functionality and the mediator adds to the existing functionality.

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

If you look at your mediator class, you see that the method handle() provides additional functionality by checking the state. If you were to take out the conditions, you would have a facade pattern, since the additional functionality is gone.

The facade pattern gives you a simple interface which interacts on a set of coherent classes. For example a remote control for your house which controls all kind of equipment in your house would be a facade. You just interact with the remote control, and the remote control figures out which device should respond and what signal to send.

The mediator pattern takes cares of communication between two objects, without the two objects need to have a reference to each other directly. A real life example is sending a letter, you post your letter and the postal service picks it up and makes sure that it will be delivered at the recipient. Without you telling them what route they should take. That is the mediator does.

Your examples however sound more like a creational pattern (looks like a factory) and a behavioral pattern (state pattern). I understand your confusion.

I have confusion in following code segment which looks similar in both the design patterns.

I think you're seeing the composition aspects of both patterns.

Facade links to various existing classes of a subsystem to add some typical functionality that simplifies use of the subsystem. In the example code you cited, ShapeMaker provides services that facilitate making shapes.

Mediator links to various colleagues that have to collaborate, so as to minimize the knowledge the colleagues have about each other. Minimizing knowledge has the side effect of reducing coupling between colleagues (they only know the mediator) and increasing their cohesion (they generally have less to worry about since they don't know about the bigger picture).

In both patterns, the centralized class assumes responsibility for the complexity of dealing with the classes it is linked to.

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

外观图案

中介模式

The Facade pattern key notes:

  1. A simple interface is required to access a complex system.
  2. The abstractions and implementations of a subsystem are tightly coupled.
  3. Need an entry point to each level of layered software.
  4. System is very complex or difficult to understand.

Related post:

What is Facade Design Pattern?

Mediator pattern key notes ( dzone article) :

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

  2. We should not use mediator pattern just to achieve lose-coupling because if the number of mediators will grow, then it will become hard to maintain them

Structure:

在此处输入图片说明

The Mediator defines the interface for communication between Colleague objects.

The ConcreteMediator implements the Mediator interface and coordinates communication between Colleague objects.

It is aware of all the Colleagues and their purpose with regards to inter communication.The ConcreteColleague communicates with other colleagues through the mediator .

Regarding your query:

  1. Your first example represents Facade pattern. ShapeMaker is entry point to complex system, which consists of various Shape sub-systems.

  2. Your second example does not represent Mediator pattern in true sense. The interactions have been hard-coded. You have to define interfaces and program to those interfaces. Refer to above dzone article for better understanding of Mediator example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM