Joydip Kanjilal
Contributor

How to implement the Builder design pattern

opinion
Nov 13, 20154 mins
Software Development

Take advantage of the Builder design pattern to create complex objects in a step by step manner

Design patterns are solutions to recurring problems and complexities in software design. The Builder design pattern falls under the creational pattern category and can be used to build a complex object using some simple objects in a step by step manner. The Builder class is independent of all other objects and is responsible for creation of the final object in a step by step manner, making it a good choice when you would need to build a complex object and need to have control over each step of the creation process.

In a typical implementation of the Builder design pattern, the Builder class is independent of the object creation process. Another class, known as the Director, is used to control how the objects would be created. This is how the GangOfFour describes the purpose of the Builder design pattern: “Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

When should I use this design pattern?

You’ll want to use the Builder design pattern when you need to have control over how complex objects would be created at runtime. Also, you should use this design pattern when you would want a step by step approach on the object creation process to be independent from the manner in which it is actually represented. In other words, you can use the Builder design pattern to isolate the creation of a complex object from the way it is actually represented. This would allow you to reuse the same creation process to build different representations.

The Builder, Factory, and Abstract Factory design patterns

There are a lot of similarities between a Builder design pattern and the Abstract Factory design pattern, which is a creational pattern that can be used to provide an interface to create instances of classes that belong to related classes without having to specify their implementations. In essence, the Abstract Factory design pattern is used to create a factory of factories.

In the Abstract Factory design pattern, the consumer invokes the Factory methods to create the objects. In the Builder design pattern, the Builder class creates an object depending on the information it receives, but the object creation process is abstracted. The Factory design pattern is a simplified form of the Builder pattern. In essence, you would want to use the Builder design pattern when you need to create a complex object in a step by step manner. When you create a simple object using just one method, you would need to take advantage of the Factory Method design pattern. The Abstract Factory design pattern is useful when you need to create objects using multiple factory methods.

Implementing the Builder Design Pattern in C#

In this section we would explore how we can implement the Builder design pattern. I’ll demonstrate how the Builder design pattern can be used to build different types of computer — laptops and desktops. Here’s what the “Product” class looks like.

public class Computer

    {

        string computerType;

        public Computer(string computerType)

        {

            this.computerType = computerType;

        }

    }

And here’s the abstract class that acts as the Builder. It comprises of a list of abstract methods — I’ve two here though you may feel free to add more. The “Builder” class named ComputerBuilder also contains a property that relates to the type of the Computer to be built.

public abstract class ComputerBuilder

    {

        public abstract void BuildOS();

        public abstract void BuildDevice();

        Computer ComputerType { get; }

    }

The following two classes represent the concrete Builder classes. I leave it up to you to write the actual implementation code for the overridden methods.

 public class LaptopBuilder : ComputerBuilder

    {

        Computer computer;

         public LaptopBuilder()

        {

            computer = new Computer("Laptop");

        }

        public override void BuildOS()

        {

            //TODO

        }

        public override void BuildDevice()

        {

            //TODO

        }

        public Computer ComputerType

        {

            get { return computer; }

        }

    }

    public class DesktopBuilder : ComputerBuilder

    {

        Computer computer;

        public DesktopBuilder()

        {

            computer = new Computer("Desktop");

        }

        public override void BuildOS()

        {

            //TODO

        }

         public override void BuildDevice()

        {

            //TODO

        }

         public Computer ComputerType

        {

            get { return computer; }

        }

    }

The Manufacturer class given next is a representation of the “Director” class.

public class Manufacturer

    {

        public void Build(ComputerBuilder computerBuilder)

        {

            computerBuilder.BuildDevice();

            computerBuilder.BuildOS();

        }

    }

The following piece of code shows how you can use the “Director” class to build the objects.

static void Main(string[] args)

        {

            Manufacturer manufacturer = new Manufacturer();

            LaptopBuilder laptopBuilder = new LaptopBuilder();

            manufacturer.Build(laptopBuilder);

        }

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author