December 18, 2014
Hot Topics:

Working With Design Patterns: Bridge

  • March 5, 2008
  • By Jeff Langr
  • Send Email »
  • More Articles »

The formatter classes hide all of the ugliness and detail behind making a report look nice, whether HTML tags or appropriate space padding is required.

Listing 5: The HTML formatter class.

public class HtmlFormatter implements Formatter {
   @Override
   public String format(String header, Detail[] details) {
      StringBuilder builder = new StringBuilder();
      builder.append(header + "<br />");

      builder.append("<table>");
      appendDetailHeader(builder, details);
      appendDetail(builder, details);
      builder.append("</table>");

      return builder.toString();
   }

   private void appendDetail(StringBuilder builder,
      Detail[] details) {
      builder.append("<tr>");
      for (int i = 0; i < details.length; i++)
         builder.append("<td>" + details[i].getValue() +
                        "</td>");
      builder.append("</tr>");
   }

   private void appendDetailHeader(StringBuilder builder,
      Detail[] details) {
      builder.append("<tr>");
      for (int i = 0; i < details.length; i++)
         builder.append("<th>" + details[i].getLabel() +
                        "</th>");
      builder.append("</tr>");
   }
}

Listing 6: The plain print formatter class.

import static util.StringUtil.*;

public class PrintFormatter implements Formatter {

   @Override
   public String format(String header, Detail[] details) {
      StringBuilder builder = new StringBuilder();
      builder.append(header + EOL);
      int longest = getLongestSize(details);
      for (int i = 0; i < details.length; i++) {
         int length = details[i].getLabel().length();
         String pad = spaces(longest - length + 1);
         builder.append(details[i].getLabel() + ":" + pad +
            details[i].getValue() + EOL);
      }
      return builder.toString();
   }

   private String spaces(int number) {
      StringBuilder builder = new StringBuilder();
      for (int i = 0; i < number; i++)
         builder.append(' ');
      return builder.toString();
   }

   private int getLongestSize(Detail[] details) {
      int longest = 0;
      for (int i = 0; i < details.length; i++)
         if (details[i].getLabel().length() > longest)
            longest = details[i].getLabel().length();
      return longest;
   }
}

The client code changes to take advantage of the bridge pattern:

Formatter formatter = new HtmlFormatter();
String result = new BookPrinter(book).print(formatter);

Short-term, implementing the bridge pattern can be overkill. Sometimes, the separation isn't worth it: A couple of classes in a couple of potentially related hierarchies might be more simply stated as four combined subclasses. But, imagine the work behind half a dozen media types and another half a dozen print formats. Look for frequency of change and common sense to be your guide.



Click here for a larger image.

Figure 1: Bridge

About the Author

Jeff Langr is a veteran software developer with over a quarter century of professional software development experience. He's written two books, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. Jeff is contributing a chapter to Uncle Bob Martin's upcoming book, Clean Code. Jeff has written over 75 articles on software development, with over thirty appearing at Developer.com. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft dot com.





Page 3 of 3



Comment and Contribute

 


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

 

 


Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel