Compare commits

...

4 Commits

Author SHA1 Message Date
Namu
ca88a009a0 Feat: We can play the code multiple times with different message
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 19s
2025-10-06 11:06:01 +02:00
Namu
9e3b3cdf58 Refactor: move strategies in a package, other minor modification 2025-10-06 11:02:38 +02:00
Namu
48136c1b7d Doc: add documentation and change the message 2025-10-06 10:55:12 +02:00
Namu
160e5d2740 Refactor: Remove unused imports and add final where it should be 2025-10-06 10:46:01 +02:00
15 changed files with 32 additions and 36 deletions

View File

@@ -2,7 +2,10 @@ import utils.*;
public class Application {
public void run() {
SendNotificationFacade.sendNotifications();
String[] messages = new String[]{"Bonjour", "You got mail", "Je préfère C#", "Fin !"};
for (final String message: messages) {
SendNotificationFacade.sendNotifications(message);
}
}
}

View File

@@ -1,25 +1,9 @@
package decorator;
import entities.Notification;
import notificationsDecorations.NotificationDecoration;
/**
* Ces approches ne seront pas implémentées ici, car elles pourraient alourdir le code sans apport clair.
* Par exemple, un builder pourrait être utile pour construire des notifications (objets complexes),
* ou des stratégies pourraient être envisagées pour des fonctionnalités comme la datation.
* Les design patterns ne sont pas des solutions universelles : leur utilisation doit être justifiée par un besoin concret.
* Le code actuel ne respecte pas pleinement les principes SOLID, ce qui peut rendre sa maintenance plus difficile.
* L'ajout de patterns comme le Singleton (considéré comme un anti-pattern dans certains contextes,
* notamment parmi les principes STUPID) ne résoudrait pas nécessairement ces problèmes.
* Pour s'entraîner, les Katas sont une excellente alternative : ce sont des exercices simples et variés,
* idéaux pour explorer les design patterns de manière propre et progressive.
* Ils permettent aussi de comparer différentes solutions pour un même problème, ce qui est très formateur !
*/
public abstract class BaseDecorator {
protected Notification notification;
protected NotificationDecoration decoration;
public BaseDecorator(Notification notification) {
this.notification = notification;

View File

@@ -1,6 +1,6 @@
package entities;
import utils.SendNotificationStrategy;
import notificationSendingStrategies.SendNotificationStrategy;
public class MailNotification extends Notification {
public MailNotification(String content, User user, SendNotificationStrategy strategy) {

View File

@@ -2,7 +2,7 @@ package entities;
import notificationsDecorations.NotificationDecoration;
import utils.NotificationState;
import utils.SendNotificationStrategy;
import notificationSendingStrategies.SendNotificationStrategy;
import java.util.ArrayList;
import java.util.List;
@@ -28,7 +28,7 @@ public abstract class Notification {
/**
* Adds a decoration to the notification. Please use a decorator for that
* @param decoration
* @param decoration The decoration you want to add
*/
public void addDecoration(NotificationDecoration decoration) {
decorations.add(decoration);
@@ -39,14 +39,17 @@ public abstract class Notification {
}
/**
* Transition d'état spéciale pour indiquer que l'envoie de notification est en échec
* Set KO state on notification
*/
public void setKoState() {
state = NotificationState.KO;
}
/**
* Fait la transition des états
* Make state transition
* PENDING -> SENT
* SENT -> READ
*
*/
public void transitionState() {
switch (state) {
@@ -55,14 +58,18 @@ public abstract class Notification {
}
}
/**
* Generates the content of the notification dynamically with the decoration (if some)
* @return content of the notification
*/
public String getContent() {
if (isSendable() && content != "") {
if (isSendable() && !content.isEmpty()) {
transitionState();
for (final NotificationDecoration decoration : decorations)
content = decoration.transformContent(content);
return content;
return strategy.makeContent(content) + "\n";
}
setKoState();
return "";

View File

@@ -1,6 +1,6 @@
package entities;
import utils.SendNotificationStrategy;
import notificationSendingStrategies.SendNotificationStrategy;
public class PushNotification extends Notification {
public PushNotification(String content, User user, SendNotificationStrategy strategy) {

View File

@@ -1,6 +1,6 @@
package entities;
import utils.SendNotificationStrategy;
import notificationSendingStrategies.SendNotificationStrategy;
public class SmsNotification extends Notification {

View File

@@ -1,7 +1,7 @@
package entities;
import utils.NotificationType;
import utils.SendNotificationStrategy;
import notificationSendingStrategies.SendNotificationStrategy;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package utils;
package notificationSendingStrategies;
public class DifferedSendNotificationStrategy implements SendNotificationStrategy {
private int delay;

View File

@@ -1,4 +1,4 @@
package utils;
package notificationSendingStrategies;
public class FastSendNotificationStrategy implements SendNotificationStrategy {
@Override

View File

@@ -1,4 +1,4 @@
package utils;
package notificationSendingStrategies;
public class SecureSendNotificationStrategy implements SendNotificationStrategy {
private int key;

View File

@@ -1,4 +1,4 @@
package utils;
package notificationSendingStrategies;
public interface SendNotificationStrategy {
String makeContent(String message);

View File

@@ -1,7 +1,5 @@
package notificationsDecorations;
import entities.Notification;
public abstract class NotificationDecoration {
/**
* Each decoration apply a transformation on the content of the Notifications

View File

@@ -1,7 +1,7 @@
package notificationsDecorations;
public class WatermarkNotificationDecoration extends NotificationDecoration {
private String waterMark;
private final String waterMark;
public WatermarkNotificationDecoration(String waterMark) {
this.waterMark = waterMark;

View File

@@ -1,6 +1,7 @@
package utils;
import entities.*;
import notificationSendingStrategies.SendNotificationStrategy;
public class NotificationFactory {
public Notification createNotification(NotificationType type, String content, User user, SendNotificationStrategy strategy) {

View File

@@ -1,6 +1,9 @@
package utils;
import entities.User;
import notificationSendingStrategies.DifferedSendNotificationStrategy;
import notificationSendingStrategies.SecureSendNotificationStrategy;
import notificationSendingStrategies.SendNotificationStrategy;
import java.util.Arrays;
@@ -9,7 +12,7 @@ import static utils.NotificationType.Mail;
import static utils.NotificationType.Sms;
public class SendNotificationFacade {
public static void sendNotifications() {
public static void sendNotifications(String message) {
var notificationManager = NotificationManager.getInstance();
NotificationType[] typesThomas = {Sms, Mail, Push};
@@ -24,6 +27,6 @@ public class SendNotificationFacade {
notificationManager.getObservable().addSubscribers(thomas);
notificationManager.getObservable().addSubscribers(alexandre);
notificationManager.getObservable().sendNotifications("C# > Java (mais Rust c'est le goat)");
notificationManager.getObservable().sendNotifications(message);
}
}