views:

131

answers:

3

Hi everyone,

I'm building an emailing system for a framework I'm developing. Thus far, I've got a generic MailMessage interface that every different type of email message will implement, something along the lines of

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
    ...
}

I've then got a SimpleMailMessage that's exactly what you would expect as a simple implementation of the interface (no encryption or encoding, just plain text).

I've created an MailMessageFactory interface that's used as an abstract factory. I've got a SimpleMailMessageFactory that implements the factory to produce instances of SimpleMailMessage.

One type of email I'd like to the framework to send is an Alert mail message, that is essentially a regular mail message except with "[Alert]" prefixed to the subject line (Another might be a email containing a "list" of order items, but I'm not sure of where the responsibility falls for converting the list to a String for an email lies). I can either subclass the SimpleMailMessage and override the setSubject(String subject) method to 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 adding the "[Alert]" string in the SimpleMailMessageFactory.

Thoughts on what I have? I think the problem is that I might be too thoughtful and forward-thinking, but I want the perfect design.

+1  A: 

The decorator seems like the better option. However, why are you writing your own email framework for Java? Why not just use the JavaMail API?

GreenieMeanie
And how JavaMail API will help us in decorating our messages. I know I have used it with Freemarker templates in order to format the email messages. And I think this question is about that. Further, I believe the original poster would be using the same, JavaMail API, anyway. No negative cast, BTW.
Adeel Ansari
+1  A: 

A decorator seems a better option to me. I am thinking of, may be you need to append your subject line with, Fwd: or Re: as well, or may be you need to support signature where you will be adding a signature to the email body.

Adeel Ansari
+1  A: 

Sounds just like the Spring's support for JavaMail. Don't reinvent the wheel, use already existing, proven solutions, and build on top of that.

Chochos
@Chochos you're right, but in my case, I need to provide different email service providers whereas Spring is constrained only to what javamail supports.
djunforgetable