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.

Huh?

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.

Class CBuilderProduct
	Private m_strProductName As String
	Private m_strLParts List As String

	Sub New (productName As String)
		m_strProductName = productName
	End Sub

	Sub Add (part As String, desc As String)
		m_strLParts (part) = desc
	End Sub

	Sub Show ()
		Dim strDisplay As String

		strDisplay = "Product: " & m_strProductName
		Forall part In m_strLParts
			strDisplay = strDisplay & Chr(13) &_
                      Listtag (part) & ": " & part
		End Forall
		Messagebox strDisplay
	End Sub
End Class

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.

Class CBuilder
	Private m_Product As CBuilderProduct

	Property Get Product As CBuilderProduct
		Set Product = m_Product
	End Property

	Sub AddOS()
	End Sub

	Sub AddPC()
	End Sub
End Class

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

Class CBuilderProductBasic As CBuilder
   Sub New()
      Set m_Product = New CBuilderProduct ("Basic Package")
   End Sub

   Sub AddOS()
      Call m_Product.Add ("Operating system", "Fedora Core 10")
   End Sub

   Sub AddPC()
      Call m_Product.Add ("Computer", "Basic home computer")
   End Sub
End Class

Class CBuilderProductPro As CBuilder
   Sub New()
      Set m_Product = New CBuilderProduct ("Pro Package")
   End Sub

   Sub AddOS()
      Call m_Product.Add ("Operating system", "Windows 7")
   End Sub

   Sub AddPC()
      Call m_Product.Add ("Computer", "Pro gaming computer")
   End Sub
End Class

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!

Class CBuilderCatalog
	Sub Create (builder As Variant)
		Call builder.AddPC()
		Call builder.AddOS()
	End Sub
End Class

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

Sub Initialize
	Dim builder As CBuilder
	Dim shop As CBuilderCatalog

	Set shop = New CBuilderCatalog ()

	Set builder = New CBuilderProductBasic ()
	Call shop.Create (builder)
	Call builder.Product.Show()

	Set builder = New CBuilderProductPro ()
	Call shop.Create (builder)
	Call builder.Product.Show()
End Sub