Skip to content

Commit f19b5c4

Browse files
Merge pull request #22 from darbyluv2code/feature/add-mediator-pattern
Add Mediator Pattern
2 parents f9b72d8 + 8a8f7a8 commit f19b5c4

File tree

10 files changed

+218
-0
lines changed

10 files changed

+218
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.luv2code</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>25</maven.compiler.source>
13+
<maven.compiler.target>25</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Role: ConcreteColleague
5+
*
6+
* Handles billing-related support tickets.
7+
*/
8+
public class BillingService implements SupportService {
9+
10+
@Override
11+
public void handleTicket(Ticket ticket) {
12+
System.out.println("[BillingService] Processing refund request for "
13+
+ ticket.getCustomerName() + ": " + ticket.getMessage());
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Defines the supported ticket types.
5+
*/
6+
public enum IssueType {
7+
BILLING,
8+
TECHNICAL
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Demonstrates the Mediator pattern
5+
*
6+
* The SupportCenter (Mediator) coordinates communication between components.
7+
*/
8+
public class MainApp {
9+
10+
static void main() {
11+
12+
System.out.println("--- Demo: Support Ticket Routing ---");
13+
14+
NotificationSender notificationSender = new NotificationSender();
15+
SupportCenter supportCenter = new SupportCenter(notificationSender);
16+
17+
supportCenter.registerService(IssueType.BILLING, new BillingService());
18+
supportCenter.registerService(IssueType.TECHNICAL, new TechnicalService());
19+
20+
Ticket billingTicket = new Ticket(
21+
IssueType.BILLING,
22+
"Refund not processed",
23+
"Alice"
24+
);
25+
26+
Ticket technicalTicket = new Ticket(
27+
IssueType.TECHNICAL,
28+
"App is crashing",
29+
"Bob"
30+
);
31+
32+
supportCenter.submitTicket(billingTicket);
33+
34+
System.out.println();
35+
36+
supportCenter.submitTicket(technicalTicket);
37+
}
38+
}
39+
40+
41+
42+
43+
44+
45+
46+
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Helper class
5+
*
6+
* Sends a confirmation message after a ticket is processed.
7+
*/
8+
public class NotificationSender {
9+
10+
public void sendConfirmation(Ticket ticket) {
11+
System.out.println("[Notification] Sent to " + ticket.getCustomerName()
12+
+ ": Your " + ticket.getType() + " ticket was received.");
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Role: ConcreteMediator
8+
*
9+
* Coordinates ticket routing and customer notification.
10+
*/
11+
public class SupportCenter implements TicketMediator {
12+
13+
private Map<IssueType, SupportService> supportServices;
14+
private NotificationSender notificationSender;
15+
16+
public SupportCenter(NotificationSender notificationSender) {
17+
this.notificationSender = notificationSender;
18+
supportServices = new HashMap<>();
19+
}
20+
21+
public void registerService(IssueType issueType, SupportService supportService) {
22+
supportServices.put(issueType, supportService);
23+
}
24+
25+
@Override
26+
public void submitTicket(Ticket ticket) {
27+
28+
System.out.println("[Mediator] Routing " + ticket.getType() + " ticket...");
29+
30+
SupportService supportService = supportServices.get(ticket.getType());
31+
32+
if (supportService == null) {
33+
throw new IllegalStateException("No service registered for: " + ticket.getType());
34+
}
35+
36+
supportService.handleTicket(ticket);
37+
notificationSender.sendConfirmation(ticket);
38+
}
39+
}
40+
41+
42+
43+
44+
45+
46+
47+
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Role: Colleague
5+
*
6+
* Defines the common behavior for support services
7+
*/
8+
public interface SupportService {
9+
10+
void handleTicket(Ticket ticket);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Role: ConcreteColleague
5+
*
6+
* Handles technical support tickets.
7+
*/
8+
public class TechnicalService implements SupportService {
9+
10+
@Override
11+
public void handleTicket(Ticket ticket) {
12+
System.out.println("[TechnicalService] Escalating to engineering for "
13+
+ ticket.getCustomerName() + ": " + ticket.getMessage());
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Domain object
5+
*
6+
* Represents a customer support ticket
7+
*/
8+
public class Ticket {
9+
10+
private IssueType type;
11+
private String message;
12+
private String customerName;
13+
14+
public Ticket(IssueType type, String message, String customerName) {
15+
this.type = type;
16+
this.message = message;
17+
this.customerName = customerName;
18+
}
19+
20+
public IssueType getType() {
21+
return type;
22+
}
23+
24+
public String getMessage() {
25+
return message;
26+
}
27+
28+
public String getCustomerName() {
29+
return customerName;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.luv2code.designpatterns.behavioral.mediator;
2+
3+
/**
4+
* Role: Mediator
5+
*
6+
* Defines the method for submitting a ticket.
7+
*/
8+
public interface TicketMediator {
9+
10+
void submitTicket(Ticket ticket);
11+
}

0 commit comments

Comments
 (0)