24
loading...
This website collects cookies to deliver better user experience
public interface Notification {
public void sendNotification(NotificationModel notif);
}
sendNotification(NotificationModel notif)
. Where NotificationModel
is a simple rudimentary POJO that will hold all the basic necessary information.public class NotificationModel {
private String to;
private String from;
private String msg;
// Getters and Setters Here ...
public NotificationModel(String to, String from, String msg) {
this.to = to;
this.from = from;
this.msg = msg;
}
@Override
public String toString() {
return msg +
" is sent to " +
to +
" from " +
from;
}
}
public class EMailNotification implements Notification {
@Override
public void sendNotification(NotificationModel notif) {
System.out.println("E-Mail Notification is going out\n" +
notif +
"\n");
}
}
public class SMSNotification implements Notification {
@Override
public void sendNotification(NotificationModel notif) {
System.out.println("SMS Notification is going out\n" +
notif +
"\n");
}
}
public class PushNotification implements Notification {
@Override
public void sendNotification(NotificationModel notif) {
System.out.println("Push Notification is going out\n" +
notif +
"\n");
}
}
NotificationType
enum so that there will be ease for the end-user while using the factory to build out Notification sub-classes.public enum NotificationType {
SMS, EMAIL, PUSH
}
public class NotificationFactory {
public Notification createNotification(NotificationType type) {
Notification notification;
switch(type) {
case EMAIL:
notification = new EMailNotification();
break;
case PUSH:
notification = new PushNotification();
break;
case SMS:
default:
notification = new SMSNotification();
break;
}
return notification;
}
}
public class Main {
public static void main(String[] args) {
NotificationModel smsNotif =
new NotificationModel("1234567895",
"1234567899",
"Hey There!");
Notification smsNotification =
new NotificationFactory()
.createNotification(NotificationType.SMS);
NotificationModel emailNotif =
new NotificationModel(
"[email protected]",
"[email protected]",
"Hey There!");
Notification emailNotification =
new NotificationFactory()
.createNotification(NotificationType.EMAIL);
NotificationModel pushNotif =
new NotificationModel("VPA12345",
"VPA90585749",
"Hey There!");
Notification pushNotification =
new NotificationFactory()
.createNotification(NotificationType.PUSH);
smsNotification.sendNotification(smsNotif);
emailNotification.sendNotification(emailNotif);
pushNotification.sendNotification(pushNotif);
}
}
SMS Notification is going out
Hey There! is sent to 1234567895 from 1234567899
E-Mail Notification is going out
Hey There! is sent to [email protected] from [email protected]
Push Notification is going out
Hey There! is sent to VPA12345 from VPA90585749
Note: when we look close to this example, we can understand this is nothing but an interface and its implementation strategy. And we use a class called factory to get the required instances. So, why go through all this pain?
We could let the customer decide the sub-class and call the required constructor, right?