How to implement the Builder design pattern

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

Builder Design Pattern

Builder Design Pattern

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);

        }

This article is published as part of the IDG Contributor Network. Want to Join?

To comment on this article and other InfoWorld content, visit InfoWorld's LinkedIn page, Facebook page and Twitter stream.
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.