September 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working With Design Patterns: Builder

  • April 21, 2008
  • By Jeff Langr
  • Send Email »
  • More Articles »

In a library, a catalog is a collection of various materials–books and movies (see Listing 1). Libraries print various reports on a frequent basis, including an inventory report that lists all materials in the library, sorted first by material type. (It's a rather large report, I'll bet!)

Listing 1: Basic materials.

// Material.java:
public class Material {
   private String classification;
   private String title;
   private String director;
   private String year;

   public Material(String classification, String title,
                   String director, String year) {
      this.classification = classification;
      this.title = title;
      this.director = director;
      this.year = year;
   }

   public String getAuthor() {
      return director;
   }

   public String getClassification() {
      return classification;
   }

   public String getTitle() {
      return title;
   }

   public String getYear() {
      return year;
   }
}

// Book.java:
public class Book extends Material {
   public Book(String classification, String title,
               String author, String year) {
      super(classification, title, author, year);
   }
   // ...
}

// Movie.java:
public class Movie extends Material {
   public Movie(String classification, String title,
                String director, String year) {
      super(classification, title, director, year);
   }

   public String getDirector() {
      return getAuthor();
   }
   // ...
}

The Catalog class is a simple container of materials. See Listing 2.

Listing 2: Catalog.

package builder;

import java.util.*;

public class Catalog implements Iterable<Material> {
   private List<Material> materials = new ArrayList<Material>();

   public void add(Material material) {
      materials.add(material);
   }

   public List<Material> materials() {
      return materials;
   }

   @Override
   public Iterator<Material> iterator() {
      return materials.iterator();
   }
}

Reports are produced using a simple construction process. First, sort the list of materials, grouping them by type (for example, books first, and then movies). Print an appropriate header for the report. Then, iterate through the sorted list of materials, and output each row. Finally, print any appropriate additional information at the end of the report.

The details behind each of these construction steps vary. For example, a web report requires appropriate HTML tags. Or perhaps a constrained-width printout (perhaps a summary receipt) requires truncating some fields and reformatting the output.

The builder pattern provides an ideal way to organize the design of such a report generator. Using the builder pattern, a client drives the construction of complex objects by specifying their type and content. The client, referred to as the "director" in the builder pattern, doesn't specify any of the implementation details—that job is left to a builder hierarchy.

Listing 3 shows the director class. The construction algorithm is expressed as one line in the generate method: The Catalog object is first sorted using the sort function, and passed to the createReport method, along with the appropriate builder object. Code in createReport makes a few calls back to the builder object: First to tell the builder to generate a header, then to tell it to generate detail output for each material object, and finally to tell it to generate a footer. This createReport method represents the "shell" of the construction algorithm.

Ultimately, the builder coughs up the report (from getReport). The director returns this report to the calling client.





Page 1 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel