From a26a8c6cf1169267cfacc44bfabcab9585ed6f25 Mon Sep 17 00:00:00 2001 From: Namu Date: Thu, 2 Oct 2025 09:35:00 +0200 Subject: [PATCH] Feat: Add facade pattern to send notification --- src/Application.java | 25 +---------------------- src/Main.java | 4 +--- src/entities/Notification.java | 10 +++++++++ src/utils/BaseDecorator.java | 13 ++++++++++++ src/utils/DateDecorator.java | 19 ++++++++++++++++++ src/utils/NotificationFacade.java | 5 ----- src/utils/PriorityDecorator.java | 17 ---------------- src/utils/ReceiptDecorator.java | 17 ---------------- src/utils/SendNotificationFacade.java | 29 +++++++++++++++++++++++++++ src/utils/TimeStampDecorator.java | 19 ------------------ src/utils/WatermarkDecorator.java | 17 ++++++++++++++++ 11 files changed, 90 insertions(+), 85 deletions(-) create mode 100644 src/utils/BaseDecorator.java create mode 100644 src/utils/DateDecorator.java delete mode 100644 src/utils/NotificationFacade.java delete mode 100644 src/utils/PriorityDecorator.java delete mode 100644 src/utils/ReceiptDecorator.java create mode 100644 src/utils/SendNotificationFacade.java delete mode 100644 src/utils/TimeStampDecorator.java create mode 100644 src/utils/WatermarkDecorator.java diff --git a/src/Application.java b/src/Application.java index 1d93be3..da0f89c 100644 --- a/src/Application.java +++ b/src/Application.java @@ -1,31 +1,8 @@ -import entities.User; import utils.*; -import java.util.Arrays; - -import static utils.NotificationType.*; - public class Application { - private final NotificationManager notificationManager; - - public Application(NotificationManager notificationManager) { - this.notificationManager = notificationManager; - } - public void run() { - NotificationType[] typesThomas = {Sms, Mail, Push}; - NotificationType[] typesAlexandre = {Sms, Mail}; - - SendNotificationStrategy secured = new SecureSendNotificationStrategy(123); - DifferedSendNotificationStrategy differed = new DifferedSendNotificationStrategy(300); - - var thomas = new User(0, "Thomas", Arrays.asList(typesThomas), secured); - var alexandre = new User(1, "Alexandre", Arrays.asList(typesAlexandre), differed); - - notificationManager.getObservable().addSubscribers(thomas); - notificationManager.getObservable().addSubscribers(alexandre); - - notificationManager.getObservable().sendNotifications("C# > Java (mais Rust c'est le goat)"); + SendNotificationFacade.sendNotifications(); } } diff --git a/src/Main.java b/src/Main.java index c68906d..c0e0777 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,8 +1,6 @@ import utils.NotificationManager; void main(String[] args) { - final var notificationManager = NotificationManager.getInstance(); - final var app = new Application(notificationManager); - + final var app = new Application(); app.run(); } diff --git a/src/entities/Notification.java b/src/entities/Notification.java index 0248e64..5bec2ea 100644 --- a/src/entities/Notification.java +++ b/src/entities/Notification.java @@ -20,5 +20,15 @@ public abstract class Notification { return this.state != NotificationState.SENT && this.state != NotificationState.READ; } + public String getContent() { + return content; + } + + /** + * Pas de change state car route non linéaire. On reste sur des enum ! + */ + + + public abstract void send(); } diff --git a/src/utils/BaseDecorator.java b/src/utils/BaseDecorator.java new file mode 100644 index 0000000..0d8ee16 --- /dev/null +++ b/src/utils/BaseDecorator.java @@ -0,0 +1,13 @@ +package utils; + +import entities.Notification; + +public abstract class BaseDecorator { + protected Notification notification; + + public BaseDecorator(Notification notification) { + this.notification = notification; + } + + public abstract void send(); +} diff --git a/src/utils/DateDecorator.java b/src/utils/DateDecorator.java new file mode 100644 index 0000000..c8c4a5d --- /dev/null +++ b/src/utils/DateDecorator.java @@ -0,0 +1,19 @@ +package utils; + +import entities.Notification; + +import java.util.Date; + +public class DateDecorator extends BaseDecorator { + private final Date date; + + public DateDecorator(Notification notification, Date date) { + super(notification); + this.date = date; + } + + @Override + public void send() { + System.out.println(this.notification.getContent() + " " + date.toString()); + } +} diff --git a/src/utils/NotificationFacade.java b/src/utils/NotificationFacade.java deleted file mode 100644 index 32c0bd0..0000000 --- a/src/utils/NotificationFacade.java +++ /dev/null @@ -1,5 +0,0 @@ -package utils; - -public class NotificationFacade { - -} diff --git a/src/utils/PriorityDecorator.java b/src/utils/PriorityDecorator.java deleted file mode 100644 index 0ec8ddb..0000000 --- a/src/utils/PriorityDecorator.java +++ /dev/null @@ -1,17 +0,0 @@ -package utils; - -public class PriorityDecorator { - private boolean isPriority; - - public PriorityDecorator(boolean isPriority) { - this.isPriority = isPriority; - } - - public boolean isPriority() { - return isPriority; - } - - public void setPriority(boolean priority) { - isPriority = priority; - } -} diff --git a/src/utils/ReceiptDecorator.java b/src/utils/ReceiptDecorator.java deleted file mode 100644 index 39c9a1b..0000000 --- a/src/utils/ReceiptDecorator.java +++ /dev/null @@ -1,17 +0,0 @@ -package utils; - -public class ReceiptDecorator { - boolean received; - - public ReceiptDecorator() { - this.received = false; - } - - public boolean isReceived() { - return received; - } - - public void setReceived(boolean received) { - this.received = received; - } -} diff --git a/src/utils/SendNotificationFacade.java b/src/utils/SendNotificationFacade.java new file mode 100644 index 0000000..9413af0 --- /dev/null +++ b/src/utils/SendNotificationFacade.java @@ -0,0 +1,29 @@ +package utils; + +import entities.User; + +import java.util.Arrays; + +import static utils.NotificationType.*; +import static utils.NotificationType.Mail; +import static utils.NotificationType.Sms; + +public class SendNotificationFacade { + public static void sendNotifications() { + var notificationManager = NotificationManager.getInstance(); + + NotificationType[] typesThomas = {Sms, Mail, Push}; + NotificationType[] typesAlexandre = {Sms, Mail}; + + SendNotificationStrategy secured = new SecureSendNotificationStrategy(123); + DifferedSendNotificationStrategy differed = new DifferedSendNotificationStrategy(300); + + var thomas = new User(0, "Thomas", Arrays.asList(typesThomas), secured); + var alexandre = new User(1, "Alexandre", Arrays.asList(typesAlexandre), differed); + + notificationManager.getObservable().addSubscribers(thomas); + notificationManager.getObservable().addSubscribers(alexandre); + + notificationManager.getObservable().sendNotifications("C# > Java (mais Rust c'est le goat)"); + } +} diff --git a/src/utils/TimeStampDecorator.java b/src/utils/TimeStampDecorator.java deleted file mode 100644 index a498e9c..0000000 --- a/src/utils/TimeStampDecorator.java +++ /dev/null @@ -1,19 +0,0 @@ -package utils; - -import java.util.Date; - -public class TimeStampDecorator { - private Date time; - - public TimeStampDecorator(Date time) { - this.time = time; - } - - public Date getTime() { - return time; - } - - public void setTime(Date time) { - this.time = time; - } -} diff --git a/src/utils/WatermarkDecorator.java b/src/utils/WatermarkDecorator.java new file mode 100644 index 0000000..be5343e --- /dev/null +++ b/src/utils/WatermarkDecorator.java @@ -0,0 +1,17 @@ +package utils; + +import entities.Notification; + +public class WatermarkDecorator extends BaseDecorator { + private final String watermark; + + public WatermarkDecorator(Notification notification, String watermark) { + super(notification); + this.watermark = watermark; + } + + @Override + public void send() { + System.out.println(this.notification.getContent() + " " + watermark); + } +}