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 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 key notes:
Related post:
What is Facade Design Pattern?
Mediator pattern key notes ( dzone article) :
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.
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:
Your first example represents Facade
pattern. ShapeMaker
is entry point to complex system, which consists of various Shape
sub-systems.
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.
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 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.