samedi 29 août 2020

Similar responsibility with different entities and better approach

I'm modeling a application that will send messages to users (sms,email, messaging apps and etc). Since i've made a similar project before, theres some problems i'm trying to avoid when modeling now. Some are:

1- The responsability for writing the message's text got along too many different classes

2- There were too many very-similar methods, like "sendWelcomeMail()" and "sendGiftMail()"

3- Message pojo accumulated way too many attributes

I'm looking ahead at those problems now and for future implementations.

That's my current implementation idea:

/** Interface that define behavior for all services reponsible for messaging */
public interface IMessageService{
   public String send(String to, String message);
   public String write(IMessage message, EventEnum events);
}


/** Example of a service*/
public class EmailService implements IMessageService{
 //... 
     @Override
     public String write(IMessage message, EventEnum event){
     if(event.getId == 1){
     return writeWecolme( (MailTemplate) message);}
     }
}


/** Interface that define a generic message template*/
public interface IMessage{}

/** Model that represent a Email message */
public class EmailTemplate implements IMessage{
 String emailAttribute;
 String otherAttribute;
 //getters and setters
}

/** Enum with possible events*/
public enum EventsEnum{
 WELCOME(1,"Welcome"), PRODUCT_SENT(2,"Product sent");
//...
}

There's some points to discuss about:

1- Is the actual IMessage a good design approach? Initially I had defined methods like toWelcome() and toProductSent(), but not all implements of a IMessage will have those behaviors

2- There's a cleaner alternative to the method write(...)? I thinks this method may get overwhelmed as Events increase

3- Should I define a POJO for each message template (eg WelcomeEmailTemplate) or a generic one for each kind of message(eg. EmailTemplate or TelegramTemplate)?

4- Should I define different interfaces for each pojo "family"?

Aucun commentaire:

Enregistrer un commentaire