What is the exact difference between 'Adapter' and 'Mediator" patterns?
Asked Answered
R

4

19

I know that Adapter is a structural pattern and Mediator is a behavioral one. But as far I understood, what both of them are doing, is connecting two (or more) other classes which are potentially incompatible (not so maintainable) for direct communication.

Can some one give a close comparison between these two and point out the exact difference?

These are the links for Adapter and Mediator explanations in TutorialsPoint.

And these are sourcemaking explanations. Adapter, Mediator.

Rinderpest answered 12/6, 2016 at 12:27 Comment(6)
If you ask me, the two examples for these supposed patterns arent very convincing or desirable ways of doing what they claim. I would look for a more authoritative source of patterns because one of the crucial aspects of documenting patterns is to show the merits of the pattern over other alternatives, something which neither of these antipatterns does.Scald
@barny could you please provide some?Rinderpest
Lol i am not an expert in patterns. There are some good patterns books.Scald
@barny Anyway I cannot link any book right? On the other hand there are number of questions which have cited tutorialspoint explanations. But if you suggest that my question should go down just because those 2 links, It is not fair right? what if I didn't cite any? And you are telling there are better articles, and when I ask you to present them, you are telling that you don't know. On the other hand if I found that kid of a GREAT explanation, I wouldn't ask that question. Anyway I added sourcemaking explanation links. Please consider. :)Rinderpest
My favorite answer is: cs.stackexchange.com/a/2290. FYI, I think every combination of, "compare pattern X with pattern Y" has been asked and answered many times on either SO or another SE community forum.Bridgettbridgette
Adapter ~= Decorator, but doesn't add responsibilities | Facade ~= Adapter, but does this for multiple Interfaces | Facade ~= Mediator, except for its intent. A Mediator can technically be a single function that takes a callback -- and 99% of the time a Mediator will deal with Business Logic as apposed to, say, utils modules which could be more of a job for Facade. They are all very similar without looking at scale, nuances, or intent.Millburn
O
22

They don't have much in common, IMO.

A mediator is used to avoid coupling several components together. Instead of each component "talking" with each other directly (and thus having to know each other and to know how to communicate all with each other), each component talks to a single object: the mediator. The name is chosen on purpose: when you're fighting with your neighbor and can't communicate with him, you go see a mediator and instead of talking to each other, you both talk with the mediator, who tries fixing the issue.

An adapter is used to "transform" an object with an interface into an object with an other interface. Just like, for example, an electrical adapter which transforms a european power outlet into an american one, so that you can use your American shaver in Europe. Simple example: you need to store a Runnable into a list of Callables. Runnable has a method run(). Callable has a method call(). You thus create an Adapter:

public class RunnableAdapter implements Callable {
    private Runnable runnable;

    public RunnableAdapter(Runnable runnable) {
        this.runnable = runnable;
    }

    public void call() {
        runnable.run();
    }
}
Ossification answered 12/6, 2016 at 12:42 Comment(3)
@JB Nizet, Thanx for your explanation. Anyway in your explanation, isn't there something that implicitly means that mediator is an adapter that supports for than 2 classes.Rinderpest
No, I wouldn't say that.Ossification
well. in both cases the user object of the mediator/adapter does not have access to the mediated/adapted target and is not aware of the impl. details of the communication with it, and the adapter/mediator does - that is enough to say they have a solid base in common. I daresay the difference you point is not even logical but philosophic.Clatter
M
15

JB Nizet already wrote a good answer. I just want to explain the differences in simpler words:

  • Mediator should be used when you don't know how to communicate with other objects or you aren't allowed to

  • Adapter should be used when you know exactly how to communicate with objects, but these objects might not support some communication methods or differ

Mobster answered 13/6, 2016 at 8:9 Comment(0)
D
5

Adapter Pattern is useful when we already have two code base one consumer code and other producer code but the format in which Consumer wish the product to be in is different from what Producer code is producing. Here as Producing code is already there in place and we do not wish to modify the existing code [ code closed for modification, open for extension ]. Adapter class can transform the product produced by Producer into format as expected by Consumer code. Format may be the APIs whose return type is different as per what Producer code and expectation of the consumer code. Adapter class uses the API of the producer code and transforms them as per the expectation of the consumer. enter image description here

Now Mediator pattern is useful when we are in process of designing the architecture or while refactoring. It helps in easy and loosely coupled interaction of objects. Define an object [Mediator] that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. enter image description here

Dorkas answered 12/6, 2016 at 16:24 Comment(0)
C
1

Can some one give a close comparison between these two and point out the exact difference?

The intent and checklist in sourcemaking links, which have been quoted in your question provides good insight.

Adapter convert the interface of a class into another interface clients expect

Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

isn't there something that implicitly means that mediator is an adapter that supports for than 2 classes. Due to this reason, Mediator can't act as an Adapter.

  1. Mediator does not transform incompatible interface to compatible interface, what client expects unlike Adapter.

  2. Mediator interacts with Collegues of same interface.

  3. Mediator abstracts/centralizes arbitrary communication between Colleague objects

Related posts with code examples:

Mediator Vs Observer Object-Oriented Design Patterns

Difference between Bridge pattern and Adapter pattern

Causerie answered 2/11, 2017 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.