Tuesday, September 1, 2009

Patterns - 016: Creational patterns ( builder)


In general, object construction details such as instantiating and initializing the components that make up the object are kept within the object, often as part ofits constructor. This type of design closely ties the object construction process with the components that make up the object.
Using the Builder pattern, the process of constructing such an object can be designed more effectively.

  • The design turns out to be more modular with each implementation contained in a different builder object.
  • Adding a new implementation (i.e., adding a new builder) becomes easier.
  • The object construction process becomes independent of the components that make up the object. This provides more control over the object construction process.
In terms of implementation, each of the different steps in the construction process can be declared as methods of a common interface to be implemented by different concrete builders.
Instead of having client objects invoke different builder methods directly, the Builder pattern suggests using a dedicated object referred to as a Director, which is responsible for invoking different builder. methods required for the construction of the final object.

The interaction between the client object, the Director and the Builder objects can be summarized as follows:

  • The client object creates instances of an appropriate concrete Builder implementer and the Director. The client may use a factory for creating an appropriate Builder object.
  • The client associates the Builder object with the Director object.
  • The client invokes the build method on the Director instance to begin the object creation process. Internally, the Director invokes different Builder methods required to construct the final object.
  • Once the object creation is completed, the client invokes the getObject method on the concrete Builder instance to get the newly created object.




Example 1



A typical online job site maintains employer-, candidate- and jobs-related data. Let us build an application using the Builder pattern that displays the necessary user interface to allow a user to search for different employers and candidates in the database. For simplicity, let us consider only three fields for each search, which users can use to specify the search criteria.

Employer Search
  • – Name
  • – City
  • – Membership Renewal Date
Candidate Search
  • – Name
  • – Experience (minimum number of years)
  • – Skill Set





  • The client first Seclect the search method ( Emplyee search or Candidate search).
  • Using the BuilderFactory, it gets the corresponding UIBuilder.
  • Create the UIDirector by passing ghe UIBuilder.
  • Invoke the UIDirector build() method to initialize the components.
  • Invoke the getSearchUI() method of the UIBuilder to get the correspoinding UI.
  • Invoke the getSQL() method of the UIBuilder to get the corresponding sql.


Example 2


Let us design the following functionality for an online shopping site.

  • A server side component receives the order information submitted by a user in the form of an XML string.
  • The order XML is then parsed and validated to create an Order object.
  • The Order object is finally saved to the disk.

S. No

Order Type

Details

1

Overseas orders


  • Orders from countries other than the United States. Additional shipping and handling is charged for these orders.
  • Overseas orders are accepted only if the order amount is greater than $100.

2

California orders


  • U.S. orders with shipping address in California and are charged additional sales tax.
  • Orders with $100 or more order amount receive free regular shipping.

3

Non-California orders


  • U.S. orders with shipping address not in California. Additional sales tax is not applicable.
  • Orders with $100 or more order amount receive free regular shipping.


The series of steps required for the creation of an Order object can be summarized as follows:
  • Parse the input XML string
  • Validate the data
  • Calculate the tax
  • Calculate the shipping
  • Create the actual object with:

  1. – Line items from the input XML string
  2. – Tax and shipping details calculated as per the details listed in Table.





  • The client first receives the order in the form of an XML record.
  • The client creates an appropriate OrderBuilder object. It then instantiates the OrderDirector, passing the OrderBuilder object as a parameter.
  • The client invokes the build method on the OrderDirector, passing the input XML data to initiate the Order object construction process. – (If a problem is encountered during the construction process, a BuilderException is thrown. In general, error handling should be done in a proper way with Builders to avoid the risk of having halfcreated objects lying around.)
  • The OrderDirector invokes different OrderBuilder methods to complete the construction of the Order object.
  • The client calls the getOrder method of the OrderBuilder to get the final constructed Order object.
  • The client can invoke the save method on the returned Order object to save the order to the disk.

No comments:

Post a Comment