Open In App
Related Articles

Mediator Design Pattern

Improve Article
Save Article
Like Article

The mediator design pattern defines an object that encapsulates how a set of objects interact. 
The Mediator is a behavioral pattern (like the Observer or the Visitor pattern) because it can change the program’s running behavior. 
We are used to see programs that are made up of a large number of classes. However, as more classes are added to a program, the problem of communication between these classes may become more complex. 
Because of this, the maintenance becomes a big problem that we need to solve in this way or another. 
Like in many other Design Patterns, The mediator pattern comes to solve the problem. It makes the communication between objects encapsulated with a mediator object. 
Objects don’t communicate directly with each other, but instead, they communicate through the mediator. 

Mediator pattern implementation in Java

This program illustrates an auction. The Auction Mediator is responsible for adding the buyers, and after each buyer bid a certain amount for the item, the mediator know who won the auction. 
Class diagram: 

Mediator design pattern


// Java code to illustrate Mediator Pattern
// All public class codes should be put in
// different files.
public interface Mediator {
    // The mediator interface
    public void addBuyer(Buyer buyer);
    public void findHighestBidder();
public class AuctionMediator implements Mediator {
    // this class implements the interface and holds
    // all the buyers in a Array list.
    // We can add buyers and find the highest bidder
    private ArrayList buyers;
    public AuctionMediator()
        buyers = new ArrayList<>();
    public void addBuyer(Buyer buyer)
        System.out.println( + " was added to" +
                "the buyers list.");
    public void findHighestBidder()
        int maxBid = 0;
        Buyer winner = null;
        for (Buyer b : buyers) {
            if (b.price > maxBid) {
                maxBid = b.price;
                winner = b;
        System.out.println("The auction winner is " + +
        ". He paid " + winner.price + "$ for the item.");
public abstract class Buyer {
    // this class holds the buyer
    protected Mediator mediator;
    protected String name;
    protected int price;
    public Buyer(Mediator med, String name)
        this.mediator = med; = name;
    public abstract void bid(int price);
    public abstract void cancelTheBid();
public class AuctionBuyer extends Buyer {
    // implementation of the bidding process
    // There is an option to bid and an option to
    // cancel the bidding
    public AuctionBuyer(Mediator mediator,
                                String name)
        super(mediator, name);
    public void bid(int price)
        this.price = price;
    public void cancelTheBid()
        this.price = -1;
public class Main {
    /* This program illustrate an auction. The AuctionMediator
    is responsible for adding the buyers, and after each
    buyer bid a certain amount for the item, the mediator
    know who won the auction. */
    public static void main(String[] args)
        AuctionMediator med = new AuctionMediator();
        Buyer b1 = new AuctionBuyer(med, "Tal Baum");
        Buyer b2 = new AuctionBuyer(med, "Elad Shamailov");
        Buyer b3 = new AuctionBuyer(med, "John Smith");
        // Create and add buyers
        System.out.println("Welcome to the auction. Tonight " +
                        "we are selling a vacation to Vegas." +
                        " please Bid your offers.");
        System.out.println("--------------------------------" +
        System.out.println("Waiting for the buyer's offers...");
        // Making bids;;;
        System.out.println("---------------------------------" +
        System.out.print( + " Has canceled his bid!, " +
                                            "in that case ");


Tal Baum was added to the buyers list.
Elad Shamailov was added to the buyers list.
John Smith was added to the buyers list.
Welcome to the auction. Tonight we are
selling a vacation to Vegas. please Bid your offers.
Waiting for the buyer's offers...
The auction winner is Elad Shamailov.
He paid 2000$ for the item.
Elad Shamailov Has canceled his bid!, In that
case The auction winner is Tal Baum.
He paid 1800$ for the item.


  • Simplicity
  • You can replace one object in the structure with a different one without affecting the classes and the interfaces.


  • The Mediator often needs to be very intimate with all the different classes, And it makes it really complex.
  • Can make it difficult to maintain.

Author :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Oct, 2023
Like Article
Save Article
Similar Reads