January 21, 2021
Hot Topics:

Working With Design Patterns: Builder

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

Listing 6: An alternate builder.

public class PrintCatalogBuilder extends CatalogReportBuilder {
   private static final String ROW_LAYOUT = "%s %s %s %s";
   private static final int YEAR_WIDTH = 4;
   private static final int CLASSIFICATION_WIDTH = 14;
   private static final int AUTHOR_WIDTH = 20;
   private static final int TITLE_WIDTH = 24;

   public void generateDetail(Material material) {

   public void generateHeader() {
      String header = format("Title", "Auth/Dir",
                             "Classification", "Year");

   private String replaceWithDashes(String header) {
      return header.replaceAll("\\S", "-");

   public void generateSummary() {

   private String format(
         String title, String author, String classification,
         String year) {
      return String.format(ROW_LAYOUT,
            pad(title, TITLE_WIDTH),
            pad(author, AUTHOR_WIDTH),
            pad(classification, CLASSIFICATION_WIDTH),
            pad(year, YEAR_WIDTH));

   private String pad(String text, int width) {
      if (text.length() >= width)
         return text.substring(0, width);

      StringBuilder spaces = new StringBuilder(text);
      for (int i = 0; i < width - text.length(); i++)
         spaces.append(' ');
      return spaces.toString();

The builder pattern is reminiscent of the Template Method pattern. When using Template Method, a base class defines an algorithm structure as well as the common details required to implement that algorithm. Derived classes complete the algorithm by "filling in the holes." The builder pattern primarily differs in that its job is to construct objects, not drive an algorithm. It also represents a "separation" of the template method—the algorithm is defined in one class (the director) but implemented in a separate builder hierarchy.

The builder pattern is a prime example of clean separation of responsibilities, as well as separation of interface and implementation. Use of the pattern also prevents what otherwise might be considerable duplication.

Click here for a larger image.

Figure 1: Builder.

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 has contributed a couple chapters 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

This article was originally published on April 21, 2008

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