Sobes.tech
Junior

What is the Mediator pattern, have you worked with it?

sobes.tech AI

Answer from AI

The Mediator pattern is a behavioral design pattern that aims to reduce the coupling between objects by allowing them to communicate through a mediator, rather than directly with each other. This simplifies communication and dependency management.

For example, in an ERP system, different modules (orders, warehouse, accounting) can communicate through a mediator that coordinates their interactions without allowing them to directly reference each other.

An example in pseudocode:

interface Mediator {
    void notify(Component sender, String event);
}

class ConcreteMediator implements Mediator {
    Component1 c1;
    Component2 c2;

    void notify(Component sender, String event) {
        if (event.equals("A")) {
            c2.doC();
        } else if (event.equals("B")) {
            c1.doA();
        }
    }
}

I have worked with this pattern when I needed to simplify interaction between modules to avoid tight coupling and facilitate code maintenance.