SDSU CS 635 Advanced Object-Oriented Design & Programming
Spring Semester, 2002
Builder
Previous    Lecture Notes Index    Next    
© 2002, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Feb-02

Contents of Doc 7, Builder



References

Design Patterns: Elements of Reusable Object-Oriented Software , Gamma, Helm, Johnson, Vlissides, Addison-Wesley, 1995, pp. 97-106

The Design Patterns Smalltalk Companion, Alpert, Brown, Woolf, 1998, pp. 47-62


Doc 7, Builder Slide # 2

Builder

Intent

Separate the construction of a complex object from its representation so that the same construction process can create different representations

Applicability


Use the Builder pattern when





Doc 7, Builder Slide # 3
Collaborations

The client creates the Director object and configures it with the desired Builder object

Director notifies the builder whenever a part of the product should be built

Builder handles requests from the director and adds parts to the product

The client retrieves the product from the builder





Doc 7, Builder Slide # 4

Example – XML Parser


Director
XML Parser

Abstract Builder Class
XML.SAXDriver (Smalltalk)
org.xml.sax.helpers.DefaultHandler (Java)
DefaultHandler (C++)

Concrete Builder Class
Your subclass of the abstract builder
Client
Your code that uses the tree built


Doc 7, Builder Slide # 5
Java Example

   public static void main(String argv[])
      {
      SAXDriverExample handler = new SAXDriverExample();
   
        // Use the default (non-validating) parser
      SAXParserFactory factory = SAXParserFactory.newInstance();
      try 
         {
         SAXParser saxParser = factory.newSAXParser();
         saxParser.parse( new File("sample"), handler );
         } 
      catch (Throwable t) 
         {
         t.printStackTrace();
         }
      System.out.println( handler.root());
      }


Doc 7, Builder Slide # 6
Smalltalk Example

| builder exampleDispatcher |

builder := SAXDriverExample new.
exampleDispatcher := SAXDispatcher new contentHandler: builder.
XMLParser 
   processDocumentInFilename: 'page' 
   beforeScanDo: 
      [:parser | 
      parser
         saxDriver:(exampleDispatcher); 
         validate: true].
builder root.


Doc 7, Builder Slide # 7

Consequences







Implementation


Builder may have to pass parts back to director, who will then pass them back to builder




Copyright ©, All rights reserved.
2002 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 21-Feb-02    Next