CSC/ECE 517 Fall 2012/ch2b 2w46 ga

From Expertiza_Wiki
Revision as of 21:44, 19 November 2012 by Ggsingh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Builder Pattern and a directory of sites

Internet has grown to be the new generation prime source of information. The objective of this page is to compile a directory of websites that discuss one of the Object Oriented Design pattern called the Builder Pattern. The aim of this wiki-page is to compile the best resources available online for studying the Builder Pattern. This page will take the reader systematically through the best articles that can be referenced at every point of study of the topic in discussion. There is also a list of interesting video tutorials and discussions on online forums regarding the Builder Design Pattern

Scenarios

The best way to learn about a Design Pattern is to begin with the scenarios that it can be implemented in. It is important to understand what real life situations and design issues necessitate the use of a pattern. It gives our study a more practical approach than simply beginning with the definitions of the patterns. This approach of studying begins with an exemplary motivation to study the pattern in the first place. There are a lot of interesting scenarios that highlight the need for the Builder Pattern, some of which are present in the following links.

Animal model making example:- http://www.codeproject.com/Articles/42415/Builder-Design-Pattern

  • This article in codeproject.com explains one simple scenario where a builder pattern is implemented. A day care teacher needs to maintain a set of moulds for creating clay animals. Each set consists of tools like head, body, tail, arm and leg of a particular animal. Each of these tools are used to create individual parts of the animal that are then compiled together to form a complete figure.

Instrument manufacturing example:- http://mahemoff.com/paper/software/gofMetaphors/builder.shtml

  • Here, we see how the Builder Pattern could be used to implement a generic Instrument manufacturing system that can be used to not only specialise the kind of instrument being built but also the kind of users interacting with the system.

Each of the other resources cited on this page also contain different scenarios in which the Builder Pattern can be implemented.

Definition

The definition by the Gang of Four states that the Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. It is a type of creational pattern that focuses on constructing complex objects step by step. This pattern is very similar to the Abstract Factory design pattern.

Typically, languages like Java provide constructors to create objects. But there are multiple problems that start creeping in if there are many different objects that take many different parameters that can be classified as either optional or mandatory.

The detailed explanations can be found at the following links:-

http://www.javacodegeeks.com/2012/07/builder-design-pattern-in-java.html

  • One interesting blog that aptly explains the builder pattern and the problems that it solves.

http://www.oodesign.com/builder-pattern.html

  • This link also provides good literature on motivations for using the builder design pattern.

Design Pattern Structure

In this pattern, a process is used to create complex objects by only specifying its type and contents. The details related to the object’s representation like the actual steps involved in creating the complex object are encapsulated. In this way, the same process can be used to create different object forms.

The pattern consists of four main parts :-

* Builder, 
* Concrete Builder, 
* Director and 
* Product.

A Builder is an interface that holds actions involved in creating the complex object. The complex object that is being created here is represented by the Product.

The Concrete Builder create and assemble the parts that constitute the Product object. They implement the Builder interface.

The Director object is responsible for constructing the Product by delegating the actual creation and assembly to the appropriate Concrete Builder.

The design pattern structure is explained quite well in the link:-

http://www.avajava.com/tutorials/lessons/builder-pattern.html?page=1

Further reading

http://java.dzone.com/news/design-pattern-builder-pattern

This link gives a good explanation of how the four modules co-ordinate together to solve the object creation problem in a clean and Object-Oriented manner.

Implementation Examples

The semantics and methods of implementing the Builder Pattern differ with the language chosen. The following links contain detailed implementation of the Builder Pattern in different scenarios

Java

http://sourcemaking.com/design_patterns/builder/java/2

The scenario of a customer ordering a pizza at a restaurant

C#

http://sourcemaking.com/design_patterns/builder/c%2523

The generic scenario

C++

http://sourcemaking.com/design_patterns/builder/cpp/1

The scenario is of distributing work over different operating systems.

PHP

http://sourcemaking.com/design_patterns/builder/php/1

This link gives the implementation of Builder pattern in PhP. The example taken here is of creating an HTML page wherein the Director gives the Builder parameters like the page title, page heading, page data among the others to construct an HTML page as the Product.

Difference between Builder Pattern and Factory Pattern

The most common confusion in Design Patterns is the distinction between Builder and the Factory Pattern. A very explanatory discussion on the difference between the two patterns that is worth following is at

http://stackoverflow.com/questions/757743/what-is-the-difference-between-builder-design-pattern-and-factory-design-pattern

Both these patterns are designed to achieve the same objective of building complex structured objects. The difference is in the approach though. Factory pattern describes an object that knows how to create several different but related kinds of object in one step, where the specific type is chosen based on given parameters.Builder pattern describes an object that knows how to craft another object of a specific type over several steps. Thus unlike factory pattern, builder pattern has a sequential step-by-step approach wherein each step has a small logic that adds value to the construction and the object will be returned as the result of the last step. A small but enough explanatory article on Factory Pattern can be found at

http://javapapers.com/design-patterns/abstract-factory-pattern/

following which the next article can clearly explain the difference between the two patterns

http://javapapers.com/design-patterns/builder-pattern/

Related concepts and terms

  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Builder often builds a Composite.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
  • Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components are built. Abstract Factory, Builder, and Prototype can useSingleton in their implementations.
  • Builders are good candidates for a fluent interface.

Video Tutorials

The video tutorials listed here carry explanations along with code implementations:-

http://www.youtube.com/watch?v=jstef_eUM6E

  • This 9 minute video tutorial by questpond.com is a conceptual explanation of the Builder Pattern with the help of 2 real life examples of tea preparation and one of generating reports. The video also illustrates a detailed Java implementation of the report generation system.

http://www.youtube.com/watch?v=9XnsOpjclUg

  • This 13 minute video tutorial by newthinktank.com illustrates a full fledged implementation of a Robot building system using the Builder Pattern.

http://www.youtube.com/watch?v=1xo_sVKcHbY

  • This 9 minute video tutorial by zaneacademy gives a thorough explaantion of the key idea behind the Builder Pattern and its basic characteristics. It then goes on to explain the UML diagram of the Builder Pattern with an example.

http://www.youtube.com/watch?v=L-iKVzW56mY&playnext=1&list=PL39AB9FE0F96FADA5&feature=results_video

  • This 9 minute video tutorial by questpond.com about the implementation of the Builder Pattern in Java. We see an in-depth analysis of a car building system and how the builder pattern is used in its implementation. The video contains a complete code run of the example and the various methods used.

http://www.youtube.com/watch?v=S9p_mwz1dd4

  • The study of design patterns is complete only when one can differentiate between the various patterns This 12 minute video

Online Discussions

These are some interesting online discussions on various issues related to the Builder Pattern

http://stackoverflow.com/questions/5211039/real-world-examples-of-the-builder-pattern

  • In this discussion on stackoverflow, the members are citing some personal coding experiences where the used the Builder Pattern and how.

http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern?lq=1

  • In this discussion on stackoverflow, the members are discussing various situations where the Builder Pattern scores over the Factory pattern, the Telescoping Constructor pattern and the JavaBean pattern. The differences between the patterns become clearer through this discussion. What is even more useful is that one of the participants of the discussion has also posted an implementation to highlight the difference.

http://stackoverflow.com/questions/4737836/can-the-builder-pattern-ever-be-doing-too-much?rq=1

  • This is an interesting discussion where members discuss how the builder pattern trades off against the factory pattern not just conceptually but in actual practice. This discussion gives hints at choosing the best design pattern for a situation and what questions one must ask oneself before finalising the design pattern.

http://stackoverflow.com/questions/5674513/differences-between-builder-pattern-vs-template-method?rq=1

  • This discussion on stackoverflow.com is about the difference between the builder pattern and the template pattern. It also highlights the crucial distinctions between other patterns as well.

http://stackoverflow.com/questions/10941464/builder-pattern-with-inheritance?rq=1

  • This is an interesting discussion of using the Builder Pattern in an inheritance hierarchy.

http://stackoverflow.com/questions/7302891/the-builder-pattern-and-a-large-number-of-mandatory-parameters?rq=1

  • This discussion on stackoverflow focusses on the general good practices that can be used while implementing the Builder pattern. Which secondary design decisions are helpful and which could raise problems during implementation. The members are each giving their own implementation practices and how they trade off when compared to other conventions followed while implementing the Builder pattern.