Builder Design Pattern
Join the DZone community and get the full member experience.
Join For Freebuilder design pattern
the builder pattern is a design pattern that allows for the step-by-step creation of complex objects using the correct sequence of actions. the construction is controlled by a director object that only needs to know the type of object it is to create.
what is the builder pattern?
the builder pattern is a gang of four design pattern . this is a creational pattern as it is to control class instantiation. the builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm. an external class, known as the director , controls the construction algorithm.
an example of this pattern could exist in a fast food restaurant system. it may be that each time a standard meal is prepared, it can include one burger or sandwich, one side order, a drink and a special offer item. the employee taking the order selects the type of meal and the builder pattern is used to determine exactly what goes into each meal.
using this model, when the employee selects a "jolly vegetarian", the ordering system determines that the meal will include a vegeburger, an order of fries, some orange juice and a voucher for a free donut. if the customer orders a "mischievous mexican", they receive a spicy burger, a side order of nachos, a shot of tequila and a free hat. in each case, the constituent parts are the same and are created in the same order. the final meal is, of course, quite different.
implementing the builder pattern
the uml class diagram above describes an implementation of the builder design pattern. the classes in the diagram are described below:
- product . the product class defines the type of the complex object that is to be generated by the builder pattern.
- builder . this abstract base class defines all of the steps that must be taken in order to correctly create a product. each step is generally abstract as the actual functionality of the builder is carried out in the concrete subclasses. the getproduct method is used to return the final product. the builder class is often replaced with a simple interface .
- concretebuilder . there may be any number of concrete builder classes inheriting from builder. these classes contain the functionality to create a particular complex product.
- director . the director class controls the algorithm that generates the final product object. a director object is instantiated and its construct method is called. the method includes a parameter to capture the specific concrete builder object that is to be used to generate the product. the director then calls methods of the concrete builder in the correct order to generate the product object. on completion of the process, the getproduct method of the builder object can be used to return the product.
the following shows the basic code of the builder design pattern implemented using c#. for brevity the properties of the product class are declared using c# 3.0 automatically implemented property syntax. these could be replaced with full definitions for earlier versions of the language:
public class director { public void construct(builder builder) { builder.buildpart1(); builder.buildpart2(); builder.buildpart3(); } } public abstract class builder { public abstract void buildpart1(); public abstract void buildpart2(); public abstract void buildpart3(); public abstract product getproduct(); } public class concretebuilder : builder { private product _product = new product(); public override void buildpart1() { _product.part1 = "part 1"; } public override void buildpart2() { _product.part2 = "part 2"; } public override void buildpart3() { _product.part3 = "part 3"; } public override product getproduct() { return _product; } } public class product { public string part1 { get; set; } public string part2 { get; set; } public string part3 { get; set; } }
example builder
to show a real-world, but very simplistic, example of the use of a design pattern, we will create two builders that generate the fast food restaurant meals described earlier in this article. each builder will create a meal with a named burger, side order, drink, special offer item and a price. in a real application, this process would probably be more complex and may require that the items be created in the correct order so that the price could be correctly calculated. the individual elements of the meal may also be complex objects in their own right.
public class mealdirector { public void makemeal(mealbuilder mealbuilder) { mealbuilder.addsandwich(); mealbuilder.addsideorder(); mealbuilder.adddrink(); mealbuilder.addofferitem(); mealbuilder.setprice(); } } public abstract class mealbuilder { public abstract void addsandwich(); public abstract void addsideorder(); public abstract void adddrink(); public abstract void addofferitem(); public abstract void setprice(); public abstract meal getmeal(); } public class jollyvegetarianmealbuilder : mealbuilder { private meal _meal = new meal(); public override void addsandwich() { _meal.sandwich = "vegeburger"; } public override void addsideorder() { _meal.sideorder = "fries"; } public override void adddrink() { _meal.drink = "orange juice"; } public override void addofferitem() { _meal.offer = "donut voucher"; } public override void setprice() { _meal.price = 4.99; } public override meal getmeal() { return _meal; } } public class mischievousmexicanbuilder : mealbuilder { private meal _meal = new meal(); public override void addsandwich() { _meal.sandwich = "spicy burger"; } public override void addsideorder() { _meal.sideorder = "nachos"; } public override void adddrink() { _meal.drink = "tequila"; } public override void addofferitem() { _meal.offer = "hat"; } public override void setprice() { _meal.price = 5.49; } public override meal getmeal() { return _meal; } } public class meal { public string sandwich { get; set; } public string sideorder { get; set; } public string drink { get; set; } public string offer { get; set; } public double price { get; set; } public override string tostring() { return string.format("{0}\n{1}\n{2}\n{3}\n{4:f2}", sandwich, sideorder, drink, offer, price); } }
testing the builder
we can test the example builder code using a console application. in the following main method, the two builders are used to create the two varieties of meal. the contents of the meals' properties are outputted to the console using the overridden tostring method .
static void main(string[] args) { mealdirector director = new mealdirector(); mealbuilder jvmb = new jollyvegetarianmealbuilder(); director.makemeal(jvmb); meal vegmeal = jvmb.getmeal(); console.writeline(vegmeal); console.writeline(); mealbuilder mmmb = new mischievousmexicanbuilder(); director.makemeal(mmmb); meal mexmeal = mmmb.getmeal(); console.writeline(mexmeal); } /* output vegeburger fries orange juice donut voucher 4.99 spicy burger nachos tequila hat 5.49 */
republished with permission from the blackwasp web site
©2006-
blackwasp all rights reserved
click for the original article .
Opinions expressed by DZone contributors are their own.
Trending
-
You’ve Got Mail… and It’s a SPAM!
-
DevOps Midwest: A Community Event Full of DevSecOps Best Practices
-
8 Data Anonymization Techniques to Safeguard User PII Data
-
Microservices: Quarkus vs Spring Boot
Comments