Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Behavioral Patterns

Mediator

Mediator provides way to implement many-to-many relationships between interacting peers. If peers interact directly between each other, it means they are tangled and we end up with spaghetti code.

Imagine if all the airplanes would try to agree who is landing first between each other, rather than contacting the air traffic control tower. Mediator is the air traffic control tower.

Example - Chat

User belongs to a group, if he wants to say something, he is always "redirected" to group. Group is the mediator, providing way users can talk together.

class Group {

    private List<User> users = new ArrayList<>();

    public void add(User user) {
        users.add(user);
    }

    public void say(String message, User author) {
        users.stream()
                .filter(user -> user != author)
                .forEach(user -> System.out.println(user.getName() + ": " + message));
    }
}

class User {

    private Group group;
    private String name;

    public User(Group group, String name) {
        this.group = group;
        this.name = name;
    }

    public Group getGroup() {
        return group;
    }

    public String getName() {
        return name;
    }

    public void say(String message) {
        group.say(message, this);
    }
}

Here is example of usage.

Group group = new Group();

User john = new User(group, "John");
group.add(john);
User jimmy = new User(group, "Jimmy");
group.add(jimmy);
User emily = new User(group, "Emily");
group.add(emily);

jimmy.say("Hi everyone!");

When we run the code, Jimmy says "Hi everyone!" and users are notified with the new message.

John: Hi everyone!
Emily: Hi everyone!
PreviousTemplate MethodNextChain Of Responsibility

Last updated 5 years ago

Was this helpful?