In part 10 of Design pattern series we will take look at Builder design pattern. Albeit much similarity, this pattern it is not to be mistaken with Abstract factory pattern. As Abstract Factory emphasizes a family of products and returns the product immediately, Builder focuses on constructing complex object step by step, returning product in final step.

The intention of the The Builder pattern is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects.


The best way to explain how builder pattern works is to look at the example. Imagine you have to build product presentation for a company that sells built PCs. For clarity’s sake, let’s pretend that they sell only two types of computers. One for basic office needs, running Linux OS and one for pro gaming needs running Windows.

Let’s go!

First, you need a product class. This class will be able to store product type, add components and display itself when needed. This allows you to build product with as many components as needed.

Next, you need a builder class. This is an abstract class, only specifying methods and variables used by end product classes that inherit from it.

Now, you are ready to create real product classes. In our case, this classes will represent Basic and Pro products.

You are almost done. But first, you need a catalogue (or shop) class. This class will actually contain algorithm for building desired products. Beware that builder parameter of Create method must be of type Variant (it should be CBuilder) or you will get an error while compiling your code that will use this design pattern!

To test the code, I wrote a simple agent that simply outputs two message boxes. One for each product.