dimanche 11 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.

I've been going through many design patterns to solve this problem. But I didn't find any such pattern to transform the fields of an object and/or convert the parent object to a child object.
The only design patterns that come close are Strategy and Decorator.
I'm using Strategy Pattern for using either EmailService or SMSService at runtime. It is solving just one part of my problem. Will Decorator Pattern help for solving the other?

Aucun commentaire:

Enregistrer un commentaire