4.5. Mediator

Maheshmaddi
2 min readApr 10, 2023

--

The Mediator pattern is a behavioral design pattern that promotes loose coupling between objects by providing a central point of communication between them. It defines an object, called the mediator, which encapsulates the interaction logic between a set of objects, making it easier to manage dependencies and maintain the code.

The Mediator pattern is typically used when:

  1. You have a set of objects that need to communicate with each other.
  2. You want to reduce the complexity and coupling between the objects.
  3. You want to encapsulate the interaction logic in a separate object, making the code more maintainable and flexible.

To implement the Mediator pattern, follow these steps:

  1. Define a mediator interface or abstract class that specifies the common methods for communicating with the objects.
  2. Create a concrete mediator class that implements the mediator interface or extends the abstract class, providing the specific interaction logic between the objects.
  3. Create classes for the objects that will communicate with each other. These classes should have a reference to the mediator and use it to send messages or notifications.
  4. Implement the communication methods in the concrete mediator class, handling the interaction between the objects.
  5. In the client code, create instances of the objects and the mediator, and associate them with each other.

Here’s a simple example of the Mediator pattern in Java:

// Mediator interface
interface Mediator {
void send(String message, Colleague colleague);
}

// Concrete mediator
class ConcreteMediator implements Mediator {
private final Colleague colleague1;
private final Colleague colleague2;

public ConcreteMediator(Colleague colleague1, Colleague colleague2) {
this.colleague1 = colleague1;
this.colleague2 = colleague2;
}

@Override
public void send(String message, Colleague colleague) {
if (colleague == colleague1) {
colleague2.receive(message);
} else {
colleague1.receive(message);
}
}
}

// Colleague class
abstract class Colleague {
protected Mediator mediator;

public Colleague(Mediator mediator) {
this.mediator = mediator;
}

public abstract void send(String message);
public abstract void receive(String message);
}

// Concrete colleagues
class Colleague1 extends Colleague {
public Colleague1(Mediator mediator) {
super(mediator);
}

@Override
public void send(String message) {
mediator.send(message, this);
}

@Override
public void receive(String message) {
System.out.println("Colleague1 received: " + message);
}
}

class Colleague2 extends Colleague {
public Colleague2(Mediator mediator) {
super(mediator);
}

@Override
public void send(String message) {
mediator.send(message, this);
}

@Override
public void receive(String message) {
System.out.println("Colleague2 received: " + message);
}
}

// Client code
public class Client {
public static void main(String[] args) {
Colleague1 colleague1 = new Colleague1(null);
Colleague2 colleague2 = new Colleague2(null);
Mediator mediator = new ConcreteMediator(colleague1, colleague2);

colleague1.mediator = mediator;
colleague2.mediator = mediator;

colleague1.send("Hello, Colleague2!");
colleague2.send("Hi, Colleague1!");
}
}

In this example, the Mediator interface defines the common methods for communicating with the objects. The ConcreteMediator class implements the Mediator interface and provides the specific interaction logic between the Colleague1 and `Colleague2

Note: For complete list of design patterns click here

--

--