samedi 10 avril 2021

Design pattern for transforming the fields of a Java Object

I have a class:

@Getter
@Setter
public class Message {

  private String sender;

  private Set<String> receivers;

  private String text;

}

The Message class can be extended by EmailMessage and SMSMessage having their own additional fields.

There are 2 services - EmailService and SMSService. They both do the following operations:

  1. Take the message object which contains sender and receiver user ids.
  2. EmailService calls another service to transform the user ids to email ids. SMSService does the same to transform the user ids to phone numbers.
  3. The Message object should be transformed to EmailMessage and SMSMEssage in their respective services.
  4. Send the EmailMessage and SMSMessage.

Which design pattern will be best suitable for the above transformation? Or is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire