Design: Email in Java
I am creating an email system for a framework that I am developing. So far I have a generic MailMessage interface that will implement every other type of email message, something like strings
public interface MailMessage {
public String[] getTo();
public void setTo(String[] to);
public String getFrom();
public void setFrom(String from);
...
// you can guess the setters/getters here
...
}
Then I got SimpleMailMessage, which is exactly what you expect as a simple interface implementation (no encryption or encoding, just text).
I created a MailMessageFactory interface that was used as an abstract factory. I have a SimpleMailMessageFactory that implements a factory to create instances of SimpleMailMessage.
One type of email that I would like to send to the framework is an Alert email, which is essentially a regular email, except that "[Alert]" is a subject line prefix (another could be an email, a list of items order, but I'm not sure where the responsibility for converting the list to String for email lies.) I can subclass SimpleMailMessage and override the setSubject (String subject) method with something like
public class AlertMailMessage {
...
public void setSuject(String subject) {
this.to = "[Alert]" + to;
}
...
}
Or I can create a decorator:
public abstract class EmailDecorator implements MailMessage {
protected MailMessage email;
...
// delegate all implemented methods to email
...
}
public class AlertEmailDecorator extends EmailDecorator {
public void setSubject(String subject) {
email.setSubject("[Alert]" + subject);
}
}
Or I can just delegate the addition of the "[Alert]" line to the SimpleMailMessageFactory.
Thoughts on what I have? I think the problem is that I may be too thoughtful and visionary, but I want the perfect design.
a source to share