July 13, 2020
Hot Topics:

Working with Design Pattern: Decorator

  • By Jeff Langr
  • Send Email »
  • More Articles »

Creating Additional Decorators

I can quickly bang out more decorators. How about the capability to send a duplicate (not a CC or BCC) of the email to another recipient? Here's a test:

import static org.junit.Assert.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

import org.junit.*;

public class DuplicatingEmailTest {
   public void create() throws AddressException,
      MessagingException {
      final Set<String> sentEmails = new HashSet<String>();
      Sendable wrappedEmail = new Email() {
         public void send(String to, String from, String subject,
            String content) {
      DuplicatingEmail email =
         new DuplicatingEmail(wrappedEmail, "jeff@langrsoft.com");
      email.send("joe@x.com", "from", "subject", "content");
      assertEquals(2, sentEmails.size());

And the implementation:

import javax.mail.*;
import javax.mail.internet.*;

public class DuplicatingEmail implements Sendable {
   private Sendable wrappedEmail;
   private final String additionalAddress;

   public DuplicatingEmail(Sendable wrappedEmail,
      String additionalAddress) {
      this.wrappedEmail = wrappedEmail;
      this.additionalAddress = additionalAddress;

   public void send(
         String toAddress, String fromAddress, String subject,
         String content)
         throws AddressException, MessagingException {
      wrappedEmail.send(toAddress, fromAddress, subject, content);
      wrappedEmail.send(additionalAddress, fromAddress, subject,

With two decorators defined, the client code can choose how to wrap Sendable implementations:

Sendable sendable =
   new DuplicatingEmail(new CensoringEmail(new Email()), "Me@z.com");
sendable.send("jane@x.com", "Joe@z.com", "yo", "the text");

The decorator pattern allows for considerable flexibility. As demonstrated here, its most useful application is when you have many potential subclasses that would create a very large number of potential combinations: DuplicatingCensoringEmail, CensoringAttachmentVerifyingEmail, and so on.

Click here for a larger image.

Figure 1: The decorator pattern.


[Gamma] Gamma, E., et. al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional, 1995.

About the Author

Jeff Langr is a veteran software developer celebrating his 25th year of professional software development. He's authored two books and dozens of published articles on software development, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. You can find out more about Jeff at his site,or you can contact him via email at jeff at langrsoft.com.

Page 2 of 2

This article was originally published on July 18, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date