Definition
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
When we have an application that need to create an object which has to be constructed using many different objects, we find our client code cluttered with the details of the various Part objects that needs to be assembled together to create the resulting object.
To illustrate on above point let us take an example of mobile phone manufacturing system. Lets assume that we have a system installed at one of the mobile phone vendors. Now the manufacturer may decide to create a phone based on parameters like Touchscreen, Operating System, Battery and Stylus. Now if we have the objects for all these parts then creation of product with any combination of above parts would lead to a very complex and unmanageable code in the client application i.e. the module that will decide what kind of phone needs to be built.
Builder pattern is meant to solve such problems. GoF defines Builder pattern as:
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
What this means is that we will have to design the system in such a way that the client application will simply specify the parameters that should be used to create the complex object and the builder will take care of building the complex object. Let us visualize the class diagram of Builder Pattern
Participants
The classes and objects participating in this pattern are:
- Builder (VehicleBuilder)
- specifies an abstract interface for creating parts of a Product object
- ConcreteBuilder (MotorCycleBuilder, CarBuilder, ScooterBuilder)
- constructs and assembles parts of the product by implementing the Builder interface
- defines and keeps track of the representation it creates
- provides an interface for retrieving the product
- Director (Shop)
- constructs an object using the Builder interface
- Product (Vehicle)
- represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
- includes classes that define the constituent parts, including interfaces for assembling the parts into the final result
Real-world code in C#
This real-world code demonstates the Builder pattern in which different vehicles are assembled in a step-by-step fashion. The Shop uses VehicleBuilders to construct a variety of Vehicles in a series of sequential steps.
using System;
using System.Collections.Generic;
namespace DoFactory.GangOfFour.Factory.RealWorld
- { /// <summary> /// MainApp startup class for Real-World /// Factory Method Design Pattern. /// </summary> class MainApp { /// <summary> /// Entry point into console application. /// </summary> static void Main() { // Note: constructors call Factory Method Document[] documents = new Document[2]; documents[0] = new Resume(); documents[1] = new Report(); // Display document pages foreach (Document document in documents) { Console.WriteLine("\n" + document.GetType().Name + "--"); foreach (Page page in document.Pages) { Console.WriteLine(" " + page.GetType().Name); } } // Wait for user Console.ReadKey(); } } /// <summary> /// The 'Product' abstract class /// </summary> abstract class Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class SkillsPage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class EducationPage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class ExperiencePage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class IntroductionPage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class ResultsPage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class ConclusionPage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class SummaryPage : Page { } /// <summary> /// A 'ConcreteProduct' class /// </summary> class BibliographyPage : Page { } /// <summary> /// The 'Creator' abstract class /// </summary> abstract class Document { private List<Page> _pages = new List<Page>(); // Constructor calls abstract Factory method public Document() { this.CreatePages(); } public List<Page> Pages { get { return _pages; } } // Factory Method public abstract void CreatePages(); } /// <summary> /// A 'ConcreteCreator' class /// </summary> class Resume : Document { // Factory Method implementation public override void CreatePages() { Pages.Add(new SkillsPage()); Pages.Add(new EducationPage()); Pages.Add(new ExperiencePage()); } } /// <summary> /// A 'ConcreteCreator' class /// </summary> class Report : Document { // Factory Method implementation public override void CreatePages() { Pages.Add(new IntroductionPage()); Pages.Add(new ResultsPage()); Pages.Add(new ConclusionPage()); Pages.Add(new SummaryPage()); Pages.Add(new BibliographyPage()); } } }
---------------------------
Vehicle Type: Scooter
Frame : Scooter Frame
Engine : none
#Wheels: 2
#Doors : 0
---------------------------
Vehicle Type: Car
Frame : Car Frame
Engine : 2500 cc
#Wheels: 4
#Doors : 4
---------------------------
Vehicle Type: MotorCycle
Frame : MotorCycle Frame
Engine : 500 cc
#Wheels: 2
#Doors : 0
Vehicle Type: Scooter
Frame : Scooter Frame
Engine : none
#Wheels: 2
#Doors : 0
---------------------------
Vehicle Type: Car
Frame : Car Frame
Engine : 2500 cc
#Wheels: 4
#Doors : 4
---------------------------
Vehicle Type: MotorCycle
Frame : MotorCycle Frame
Engine : 500 cc
#Wheels: 2
#Doors : 0
No comments:
Post a Comment