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.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 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. – DisappointedByUnaccountableMod Jun 12 '16 at 12:42
  • @barny could you please provide some? – Supun Wijerathne Jun 12 '16 at 12:46
  • Lol i am not an expert in patterns. There are some good patterns books. – DisappointedByUnaccountableMod Jun 12 '16 at 12:48
  • @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. :) – Supun Wijerathne Jun 13 '16 at 03:11
  • My favorite answer is: http://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. – jaco0646 Jun 13 '16 at 19:59
  • 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*. – Cody Jun 22 '16 at 21:11

4 Answers4

21

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();
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • @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. – Supun Wijerathne Jun 13 '16 at 03:19
  • No, I wouldn't say that. – JB Nizet Jun 13 '16 at 07:16
  • 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. – Radagast the Brown Nov 14 '21 at 12:56
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

CrimsonSage
  • 217
  • 3
  • 12
Dzianis Yafimau
  • 2,034
  • 1
  • 27
  • 38
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

Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48
nits.kk
  • 5,204
  • 4
  • 33
  • 55
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

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211