Feat: Add facade pattern to send notification
This commit is contained in:
@@ -1,31 +1,8 @@
|
|||||||
import entities.User;
|
|
||||||
import utils.*;
|
import utils.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import static utils.NotificationType.*;
|
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
private final NotificationManager notificationManager;
|
|
||||||
|
|
||||||
public Application(NotificationManager notificationManager) {
|
|
||||||
this.notificationManager = notificationManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
NotificationType[] typesThomas = {Sms, Mail, Push};
|
SendNotificationFacade.sendNotifications();
|
||||||
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)");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import utils.NotificationManager;
|
import utils.NotificationManager;
|
||||||
|
|
||||||
void main(String[] args) {
|
void main(String[] args) {
|
||||||
final var notificationManager = NotificationManager.getInstance();
|
final var app = new Application();
|
||||||
final var app = new Application(notificationManager);
|
|
||||||
|
|
||||||
app.run();
|
app.run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,5 +20,15 @@ public abstract class Notification {
|
|||||||
return this.state != NotificationState.SENT && this.state != NotificationState.READ;
|
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();
|
public abstract void send();
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/utils/BaseDecorator.java
Normal file
13
src/utils/BaseDecorator.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
19
src/utils/DateDecorator.java
Normal file
19
src/utils/DateDecorator.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package utils;
|
|
||||||
|
|
||||||
public class NotificationFacade {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
29
src/utils/SendNotificationFacade.java
Normal file
29
src/utils/SendNotificationFacade.java
Normal file
@@ -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)");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
17
src/utils/WatermarkDecorator.java
Normal file
17
src/utils/WatermarkDecorator.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user