dcsimg
June 18, 2018
Hot Topics:

Working with Design Pattern: Decorator

  • July 18, 2007
  • 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 {
   @Test
   public void create() throws AddressException,
      MessagingException {
      final Set<String> sentEmails = new HashSet<String>();
      Sendable wrappedEmail = new Email() {
         @Override
         public void send(String to, String from, String subject,
            String content) {
            sentEmails.add(to);
         }
      };
      DuplicatingEmail email =
         new DuplicatingEmail(wrappedEmail, "jeff@langrsoft.com");
      email.send("joe@x.com", "from", "subject", "content");
      assertEquals(2, sentEmails.size());
      assertTrue(sentEmails.contains("joe@x.com"));
      assertTrue(sentEmails.contains("jeff@langrsoft.com"));
   }
}

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,
                        content);
   }
}

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.

Reference

[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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

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

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date