Mediator Design Pattern in Java Last Updated : 06 Dec, 2023 Comments Improve Suggest changes Like Article Like Report 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: Java // 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<>(); } @Override public void addBuyer(Buyer buyer) { buyers.add(buyer); System.out.println(buyer.name + " was added to" + "the buyers list."); } @Override 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 " + winner.name + ". 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; this.name = 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); } @Override public void bid(int price) { this.price = price; } @Override 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 med.addBuyer(b1); med.addBuyer(b2); med.addBuyer(b3); 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 b1.bid(1800); b2.bid(2000); b3.bid(780); System.out.println("---------------------------------" + "--------------"); med.findHighestBidder(); b2.cancelTheBid(); System.out.print(b2.name + " Has canceled his bid!, " + "in that case "); med.findHighestBidder(); } } Output: 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.Advantages: SimplicityYou can replace one object in the structure with a different one without affecting the classes and the interfaces.Disadvantages: 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 : http://designpattern.co.il/ Comment D DesignPattern Follow 0 Improve D DesignPattern Follow 0 Improve Article Tags : Design Pattern System Design Explore What is System DesignSystem Design Introduction - LLD & HLD7 min readSystem Design Life Cycle | SDLC (Design)7 min readWhat are the components of System Design?10 min readGoals and Objectives of System Design5 min readWhy is it Important to Learn System Design?6 min readImportant Key Concepts and Terminologies â Learn System Design9 min readAdvantages of System Design4 min readSystem Design FundamentalsAnalysis of Monolithic and Distributed Systems - Learn System Design10 min readRequirements Gathering in System Design6 min readDifferences between System Analysis and System Design4 min readHorizontal and Vertical Scaling | System Design5 min readCapacity Estimation in Systems Design10 min readHow to Answer a System Design Interview Problem/Question?5 min readFunctional and Non Functional Requirements5 min readWeb Server, Proxies and their role in Designing Systems9 min readScalability in System DesignWhat is Scalability and How to achieve it?7 min readWhich Scalability approach is right for our Application? - System Design4 min readPrimary Bottlenecks that Hurt the Scalability of an Application - System Design4 min readDatabases in Designing SystemsComplete Guide to Database Design - System Design11 min readSQL vs. NoSQL - Which Database to Choose in System Design?5 min readFile and Database Storage Systems in System Design4 min readBlock, Object, and File Storage in System Design6 min readDatabase Sharding - System Design8 min readDatabase Replication in System Design6 min readHigh Level Design(HLD)What is High Level Design? - Learn System Design9 min readAvailability in System Design5 min readConsistency in System Design8 min readReliability in System Design5 min readCAP Theorem in System Design5 min readWhat is API Gateway?7 min readWhat is Content Delivery Network(CDN) in System Design7 min readWhat is Load Balancer & How Load Balancing works?4 min readCaching - System Design Concept8 min readCommunication Protocols in System Design6 min readActivity Diagrams - Unified Modeling Language (UML)10 min readMessage Queues - System Design12 min readLow Level Design(LLD)What is Low Level Design or LLD?6 min readAuthentication vs Authorization in LLD - System Design3 min readPerformance Optimization Techniques for System Design3 min readObject-Oriented Analysis and Design(OOAD)6 min readData Structures and Algorithms for System Design6 min readContainerization Architecture in System Design10 min readModularity and Interfaces In System Design8 min readUnified Modeling Language (UML) Diagrams8 min readData Partitioning Techniques in System Design5 min readHow to Prepare for Low-Level Design Interviews?4 min readEssential Security Measures in System Design8 min readDesign PatternsDesign Patterns Tutorial9 min readCreational Design Patterns4 min readStructural Design Patterns7 min readBehavioral Design Patterns5 min readDesign Patterns Cheat Sheet - When to Use Which Design Pattern?7 min readInterview Guide for System DesignHow to Crack System Design Interview Round?9 min readSystem Design Interview Questions and Answers1 min read5 Common System Design Concepts for Interview Preparation12 min read5 Tips to Crack Low-Level System Design Interviews6 min readSystem Design Interview Questions & AnswersMost Commonly Asked System Design Interview Problems/Questions1 min readDesign Dropbox - A System Design Interview Question14 min readDesigning Twitter - A System Design Interview Question15+ min readSystem Design Netflix | A Complete Architecture14 min readSystem Design of Uber App | Uber System Architecture13 min readDesign BookMyShow - A System Design Interview Question10 min readDesigning Facebook Messenger | System Design Interview9 min readComplete Roadmap to Learn System Design for Beginners6 min readGuide to System Design for Freshers15+ min readHow Disney+ Hotstar Managed (5 Cr)+ Live Viewers During India's T20 World Cup Win[2024]8 min read Like