0% found this document useful (0 votes)
14 views3 pages

Lab 15

The mediator pattern defines an object that acts as an intermediary between users and the mail server. The email mediator centralizes communication and coordinates interactions between users and the mail server. Users send emails through the mediator, which then transmits them to the mail server. The mediator also receives new emails from the server and notifies the relevant users. This provides a more flexible and extensible email system by centralizing interactions through the mediator object.

Uploaded by

daryn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Lab 15

The mediator pattern defines an object that acts as an intermediary between users and the mail server. The email mediator centralizes communication and coordinates interactions between users and the mail server. Users send emails through the mediator, which then transmits them to the mail server. The mediator also receives new emails from the server and notifies the relevant users. This provides a more flexible and extensible email system by centralizing interactions through the mediator object.

Uploaded by

daryn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Documentation

Group: IT1-2107
Name: Seidygali Daryn

Mediator Pattern

Documentation:

Objects:

Users: Each employee in the organization is represented by a "User"


object. Each user can send and receive emails.

Mail Server (Mail Server): It is a central mail server that receives, sends
and stores emails.

E-mail Mediator: A class that acts as an intermediary between users and


the mail server. It manages the sending and receiving of emails, providing
centralized interaction.

The scheme of interaction:


Users send emails through an intermediary, which then transmits them to
the mail server.

The intermediary is also responsible for receiving new emails from the
server and notifying users of new messages.

Using the Mediator Template:


The user sends requests (emails) via an E-mail Mediator, which
coordinates and forwards them to the Mail Server.

The E-mail Mediator also receives notifications of new emails from the
Mail Server and notifies the relevant users.

In this example, the E-mail Mediator acts as an intermediary between the


User and the Mail server. It centralizes the sending and receiving of
emails, providing a more flexible and extensible email system.
Code:
public class User {
private String username;

public User(String username) {


this.username = username;
}

public void sendEmail(EmailMediator mediator, String recipient, String


message) {
mediator.sendEmail(this, recipient, message);
}

public void receiveEmail(String sender, String message) {


System.out.println(username + " received an email from " + sender +
": " + message);
}
}

// MailServer.java
public class MailServer {
public void sendEmail(String sender, String recipient, String message)
{

System.out.println("Email sent from " + sender + " to " + recipient


+ ": " + message);
}
}

import java.util.HashMap;
import java.util.Map;

public class EmailMediator {


private MailServer mailServer;
private Map<String, User> users;

public EmailMediator(MailServer mailServer) {


this.mailServer = mailServer;
this.users = new HashMap<>();
}

public void registerUser(User user) {


users.put(user.getUsername(), user);
}

public void sendEmail(User sender, String recipient, String message) {


mailServer.sendEmail(sender.getUsername(), recipient, message);
}

public void receiveEmail(String sender, String recipient, String


message) {
User user = users.get(recipient);
if (user != null) {
user.receiveEmail(sender, message);
}
}
}
public class Main {
public static void main(String[] args) {
MailServer mailServer = new MailServer();
EmailMediator emailMediator = new EmailMediator(mailServer);

User alice = new User("Name1");


User bob = new User("Name2");

emailMediator.registerUser(alice);
emailMediator.registerUser(bob);

alice.sendEmail(emailMediator, "Name1", "Hello, Bob! How are


you?");
bob.sendEmail(emailMediator, "Nmae2", "Hi Alice! I'm doing well.");

}
}

Diagram:

You might also like