
Java programmer expert
Today we will look at another Java design pattern from the category of behavioural patterns – Mediator. Design patterns in this category deal with the interaction between objects and their responsibility.
The Mediator design pattern is used to simplify communication between objects in a system by providing a central point for communication between objects. The mediator plays the role of an intermediary and ensures that objects do not communicate directly, which keeps the system easily extensible and maintainable.
The problem it addresses is that in complex systems, communication between objects can become complicated and unclear. The aim of this pattern is to ensure that objects do not know each other and are therefore not dependent on each other, and that communication between objects does not take place directly, but through a single mediator. This minimizes the binding of objects to each other and makes it easier to change or add new relationships, as all communication goes through a mediator.
We will now demonstrate how the Mediator design pattern can simplify communication between objects and remove unnecessary dependencies between them by programming a simple chat room that allows participants in a discussion to communicate via both public and private messages.
Mediator.java
package designpatterns;
public interface Mediator {
void poslatSpravu(Diskuter od, String sprava);
void poslatSukromnuSpravu(Diskuter odkoho, Diskuter komu, String sprava);
}
The Mediator interface defines methods for sending messages to all participants in a discussion and for sending private messages to a specific participant.
Chat.java
package designpatterns;
import java.util.ArrayList;
import java.util.List;
public class Chat implements Mediator {
private List<Diskuter> diskutujuci;
public Chat() {
this.diskutujuci = new ArrayList<>();
}
public void pridat(Diskuter diskuter) {
diskutujuci.add(diskuter);
}
@Override
public void poslatSpravu(Diskuter od, String sprava) {
for(Diskuter prijmatel : diskutujuci) {
// Poslanie správy všetkým okrem odosielateľa.
if(prijmatel != od) {
prijmatel.prijatSpravu(sprava);
}
}
}
@Override
public void poslatSukromnuSpravu(Diskuter od, Diskuter komu, String sprava) {
for(Diskuter prijmatel : diskutujuci) {
// Poslanie správy konkrétnemu príjmateľovi.
if(prijmatel == komu) {
prijmatel.prijatSukromnuSpravu(od, sprava);
}
}
}
}
The Chat class provides an implementation of the Mediator interface. It contains a list of discussants and ensures that messages are sent either to all discussants or to specific discussants.
Discussant.java
package designpatterns;
public interface Diskuter {
void poslatSpravu(String sprava);
void prijatSpravu(String sprava);
void poslatSukromnuSpravu(Diskuter komu, String sprava);
void prijatSukromnuSpravu(Diskuter odkoho, String sprava);
}
The Discussant interface defines methods for sending and receiving public and private messages.
Member.java
package designpatterns;
public class Clen implements Diskuter {
private String meno;
private Chat chat;
public Clen(String meno, Chat chat) {
this.meno = meno;
this.chat = chat;
chat.pridat(this);
}
@Override
public void poslatSpravu(String sprava) {
System.out.println(meno + " posiela správu: " + sprava);
chat.poslatSpravu(this, sprava);
}
@Override
public void prijatSpravu(String sprava) {
System.out.println(meno + " prijal/a správu: " + sprava);
}
@Override
public void poslatSukromnuSpravu(Diskuter komu, String sprava) {
System.out.println(meno + " posiela súkromnú správu: " + sprava);
chat.poslatSukromnuSpravu(this, komu, sprava);
}
@Override
public void prijatSukromnuSpravu(Diskuter odkoho, String sprava) {
System.out.println(meno + " prijal/a súkromnú správu: " + sprava);
}
}
The Member class implements the Discussant interface. Each member has a name and a chat to which they are added. They can send messages to everyone, receive messages and send private messages.
Main.java
import designpatterns.Chat;
import designpatterns.Clen;
import designpatterns.Diskuter;
public class Main {
public static void main(String[] args) {
Chat chat = new Chat();
Diskuter clen1 = new Clen("Jozef", chat);
Diskuter clen2 = new Clen("Petra", chat);
Diskuter clen3 = new Clen("Dominik", chat);
Diskuter clen4 = new Clen("Karolina", chat);
clen1.poslatSpravu("Ahoj všetci!");
clen4.poslatSukromnuSpravu(clen1, "Ahoj Jozef!");
clen3.poslatSpravu("Idete si zahrať stolný futbal?");
clen1.poslatSpravu("Jasné, prečo nie.");
clen2.poslatSpravu("Mám prácu, ale jednu si zahrám.");
clen4.poslatSpravu("Ja sa pridám.");
clen3.poslatSpravu("Za minútku dole.");
clen2.poslatSukromnuSpravu(clen1, "Vyklepeme ich ako rezne :-D");
}
}
The Main class creates a chat object and adds some chat members to it. It then demonstrates the functionality of chat – communication between members, such as sending and receiving messages, including private messages.
There are no fixed links between the objects (members of the discussion), all communication takes place through an intermediary – the chat. Adding new members to the discussion is just as easy and doesn’t increase the complexity of communication.
The output of this example is:
The Mediator design pattern is often used when we want to simplify communication and messaging between objects by centralizing communication. The complexity of a system increases with the number of dependencies and relationships between objects; this design pattern helps to significantly reduce them.
We have prepared the files with the above example in the form of code that you can run directly in Java. You can download the Java Mediator code here.
If you’re a Java developer looking for work, check out our employee benefits and respond to our job offers!