Compare commits
4 Commits
53cda5688f
...
ca88a009a0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca88a009a0 | ||
|
|
9e3b3cdf58 | ||
|
|
48136c1b7d | ||
|
|
160e5d2740 |
@@ -2,7 +2,10 @@ import utils.*;
|
|||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
public void run() {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,9 @@
|
|||||||
package decorator;
|
package decorator;
|
||||||
|
|
||||||
import entities.Notification;
|
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 {
|
public abstract class BaseDecorator {
|
||||||
protected Notification notification;
|
protected Notification notification;
|
||||||
protected NotificationDecoration decoration;
|
|
||||||
|
|
||||||
public BaseDecorator(Notification notification) {
|
public BaseDecorator(Notification notification) {
|
||||||
this.notification = notification;
|
this.notification = notification;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package entities;
|
package entities;
|
||||||
|
|
||||||
import utils.SendNotificationStrategy;
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
public class MailNotification extends Notification {
|
public class MailNotification extends Notification {
|
||||||
public MailNotification(String content, User user, SendNotificationStrategy strategy) {
|
public MailNotification(String content, User user, SendNotificationStrategy strategy) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package entities;
|
|||||||
|
|
||||||
import notificationsDecorations.NotificationDecoration;
|
import notificationsDecorations.NotificationDecoration;
|
||||||
import utils.NotificationState;
|
import utils.NotificationState;
|
||||||
import utils.SendNotificationStrategy;
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -28,7 +28,7 @@ public abstract class Notification {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a decoration to the notification. Please use a decorator for that
|
* 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) {
|
public void addDecoration(NotificationDecoration decoration) {
|
||||||
decorations.add(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() {
|
public void setKoState() {
|
||||||
state = NotificationState.KO;
|
state = NotificationState.KO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fait la transition des états
|
* Make state transition
|
||||||
|
* PENDING -> SENT
|
||||||
|
* SENT -> READ
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public void transitionState() {
|
public void transitionState() {
|
||||||
switch (state) {
|
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() {
|
public String getContent() {
|
||||||
if (isSendable() && content != "") {
|
if (isSendable() && !content.isEmpty()) {
|
||||||
transitionState();
|
transitionState();
|
||||||
|
|
||||||
for (final NotificationDecoration decoration : decorations)
|
for (final NotificationDecoration decoration : decorations)
|
||||||
content = decoration.transformContent(content);
|
content = decoration.transformContent(content);
|
||||||
|
|
||||||
return content;
|
return strategy.makeContent(content) + "\n";
|
||||||
}
|
}
|
||||||
setKoState();
|
setKoState();
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package entities;
|
package entities;
|
||||||
|
|
||||||
import utils.SendNotificationStrategy;
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
public class PushNotification extends Notification {
|
public class PushNotification extends Notification {
|
||||||
public PushNotification(String content, User user, SendNotificationStrategy strategy) {
|
public PushNotification(String content, User user, SendNotificationStrategy strategy) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package entities;
|
package entities;
|
||||||
|
|
||||||
import utils.SendNotificationStrategy;
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
public class SmsNotification extends Notification {
|
public class SmsNotification extends Notification {
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package entities;
|
package entities;
|
||||||
|
|
||||||
import utils.NotificationType;
|
import utils.NotificationType;
|
||||||
import utils.SendNotificationStrategy;
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils;
|
package notificationSendingStrategies;
|
||||||
|
|
||||||
public class DifferedSendNotificationStrategy implements SendNotificationStrategy {
|
public class DifferedSendNotificationStrategy implements SendNotificationStrategy {
|
||||||
private int delay;
|
private int delay;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils;
|
package notificationSendingStrategies;
|
||||||
|
|
||||||
public class FastSendNotificationStrategy implements SendNotificationStrategy {
|
public class FastSendNotificationStrategy implements SendNotificationStrategy {
|
||||||
@Override
|
@Override
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils;
|
package notificationSendingStrategies;
|
||||||
|
|
||||||
public class SecureSendNotificationStrategy implements SendNotificationStrategy {
|
public class SecureSendNotificationStrategy implements SendNotificationStrategy {
|
||||||
private int key;
|
private int key;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils;
|
package notificationSendingStrategies;
|
||||||
|
|
||||||
public interface SendNotificationStrategy {
|
public interface SendNotificationStrategy {
|
||||||
String makeContent(String message);
|
String makeContent(String message);
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package notificationsDecorations;
|
package notificationsDecorations;
|
||||||
|
|
||||||
import entities.Notification;
|
|
||||||
|
|
||||||
public abstract class NotificationDecoration {
|
public abstract class NotificationDecoration {
|
||||||
/**
|
/**
|
||||||
* Each decoration apply a transformation on the content of the Notifications
|
* Each decoration apply a transformation on the content of the Notifications
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package notificationsDecorations;
|
package notificationsDecorations;
|
||||||
|
|
||||||
public class WatermarkNotificationDecoration extends NotificationDecoration {
|
public class WatermarkNotificationDecoration extends NotificationDecoration {
|
||||||
private String waterMark;
|
private final String waterMark;
|
||||||
|
|
||||||
public WatermarkNotificationDecoration(String waterMark) {
|
public WatermarkNotificationDecoration(String waterMark) {
|
||||||
this.waterMark = waterMark;
|
this.waterMark = waterMark;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package utils;
|
package utils;
|
||||||
|
|
||||||
import entities.*;
|
import entities.*;
|
||||||
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
public class NotificationFactory {
|
public class NotificationFactory {
|
||||||
public Notification createNotification(NotificationType type, String content, User user, SendNotificationStrategy strategy) {
|
public Notification createNotification(NotificationType type, String content, User user, SendNotificationStrategy strategy) {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package utils;
|
package utils;
|
||||||
|
|
||||||
import entities.User;
|
import entities.User;
|
||||||
|
import notificationSendingStrategies.DifferedSendNotificationStrategy;
|
||||||
|
import notificationSendingStrategies.SecureSendNotificationStrategy;
|
||||||
|
import notificationSendingStrategies.SendNotificationStrategy;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -9,7 +12,7 @@ import static utils.NotificationType.Mail;
|
|||||||
import static utils.NotificationType.Sms;
|
import static utils.NotificationType.Sms;
|
||||||
|
|
||||||
public class SendNotificationFacade {
|
public class SendNotificationFacade {
|
||||||
public static void sendNotifications() {
|
public static void sendNotifications(String message) {
|
||||||
var notificationManager = NotificationManager.getInstance();
|
var notificationManager = NotificationManager.getInstance();
|
||||||
|
|
||||||
NotificationType[] typesThomas = {Sms, Mail, Push};
|
NotificationType[] typesThomas = {Sms, Mail, Push};
|
||||||
@@ -24,6 +27,6 @@ public class SendNotificationFacade {
|
|||||||
notificationManager.getObservable().addSubscribers(thomas);
|
notificationManager.getObservable().addSubscribers(thomas);
|
||||||
notificationManager.getObservable().addSubscribers(alexandre);
|
notificationManager.getObservable().addSubscribers(alexandre);
|
||||||
|
|
||||||
notificationManager.getObservable().sendNotifications("C# > Java (mais Rust c'est le goat)");
|
notificationManager.getObservable().sendNotifications(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user