Builder Pattern in VB.NET

 Builder is an object creational design pattern that codifies the construction process outside of the actual steps that carries out the construction - thus allowing the construction process itself to be reused.

Non-software Example

Fast food restaurants to construct children's meals use this pattern. Children's meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy car). Note that there can be variation in the content of the children's meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants. 

Another example for Builder pattern is a Computer Assembly. A computer is nothing but the bundling of various components like FDD, HDD, and Monitor etc. But when an user buys a computer someone assemble all these components and given to us. Remember that here the building process is completely hidden from the client or user.  

The UML diagram for a Builder pattern is more or less like following one.  

BuilderPatterns-Vb.net.gif

Remember that a project can contain one or more builders and each builder is independent of others. This will improves the modularity and makes the addition of other builders relatively simple. Since each builder constructs the final product step by step, we have more control over the final product that a builder constructs. 

VB.NET Implementation

'Creational Pattern: BUILDER
'Author: [email protected] 
Imports System
Class Director
Public Sub Construct(ByVal builder As IBuilder)
builder.DoIt()
End Sub 'Construct
End Class 'Director
Interface IBuilder
Sub DoIt()
End Interface 'IBuilder
Class BuilderA
Implements IBuilder
Public Sub DoIt()
'Necessary code for building the computer type A
Console.WriteLine("Assembly a Computer with mono monitor")
End Sub 'DoIt
End Class 'BuilderA
Class BuilderB
Implements IBuilder 'ToDo: Add Implements Clauses for implementation methods of these interface(s)
Public Sub
 DoIt()
'Necessary code for building the computer type B
Console.WriteLine("Assembly a Computer with color monitor")
End Sub 'DoIt
End Class 'BuilderB
_
Class MyClient
Public Shared Sub Main()
Dim d As New Director
Dim build = New BuilderA
d.Construct(build)
End Sub 'Main
End Class 'MyClient

Up Next
    Ebook Download
    View all
    Learn
    View all