Currently i got an entity class:
public class Notification {
private NotificationType type;
private LocalDateTime sentDate;
private String message;
private String imageUrl;
private String refId;
}
And an enum:
public enum NotificationType {
INVITATION_TO_GROUP("INVITATION_TO_GROUP"),
LEAVING_GROUP("LEAVING_GROUP"),
QUESTION_ANSWERED("QUESTION_ANSWERED"),
NEW_OWNER("NEW_OWNER");
private String value;
NotificationType(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
}
I want to create notifications based on enum value so i created a simple method in my notification service:
public <T> Notification createNotification(NotificationType type, T obj) {
Notification response = null;
GroupRequest request;
switch(type) {
case INVITATION_TO_GROUP:
request = ((GroupRequest) obj);
response = new Notification(
NotificationType.INVITATION_TO_GROUP,
...
request.getId().getUser()
);
break;
case LEAVING_GROUP:
request = ((GroupRequest) obj);
response = new Notification(
NotificationType.LEAVING_GROUP,
...
request.getId().getGroup().getOwner()
);
break;
case NEW_OWNER:
Group group = ((Group) obj);
response = new Notification(
NotificationType.NEW_OWNER,
...
group.getOwner()
);
break;
case QUESTION_ANSWERED:
//??
break;
default:
break;
}
return response;
}
As you can see i got one generic parameter for diffrent objects that i use to initialize Notification objects. It works for first 3 enum values but for QUESTION_ANSWERED value besides passing an object like in previous 3 cases i need to pass also a String value and the whole method would look even more ugly than now. I think i need to use Factory desing pattern but i really can't fit which one to use for diffrent number of parameters. Do i need to create simple factory interface with methods like createNotificationForInvitation, createNotificationForQuestionAnswered etc. for each enum value with diffrent parameters and all of them will return Notification objects? How to make it look more pretty? :)