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

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


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 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


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