Easy learning with example program codes

java mediator design pattern

Dictionary meaning of mediator: a person who attempts to make people involved in a conflict come to an agreement.

Java mediator design pattern comes under behavioural design patterns. Mediator design pattern is used to provide the loose coupling or to reduce the interaction complexity between multiple objects.

Let’s discuss mediator design pattern with below example. We are considering group chat application. A user can send and receive the messages. If a user send a message, it should be received by all other members in the group.


package com.codesjava;
public class ChatMediator {
	public static void displayMessage(String msg, User user){
		System.out.println("User: " + user.getName() + ", Message: " + msg);

package com.codesjava;
public class User {
   private String name;
   public String getName() {
      return name;
   public void setName(String name) { = name;
   public User(String name){  = name;
   public void sendMessage(String message){

package com.codesjava;
public class MediatorPatternTest {
   public static void main(String args[]){
      User bharat = new User("Bharat");
      User sahdev = new User("Sahdev");
      User richi = new User("Richi");
      bharat.sendMessage("Hello Richi");
      richi.sendMessage("Hello Bharat");
      sahdev.sendMessage("Hi all");


User: Bharat, Message: Hello Richi
User: Richi, Message: Hello Bharat
User: Sahdev, Message: Hi all

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki