Builder Design Pattern in Visual Basic .NET

September 1, 2017

This pattern comes under Creational Pattern and this is used to create object. You can build complex object using simple objects. To understand the Creational Pattern we can think of a restaurant where you have kids meal menu and adult meal menu.

Now you can think that our Meal class is little bit complex because it has several items. So we build our meal by adding items to final meal. and also we have meals for adults and kids too

 

Meal Class

Public Class Meal
    Dim m_soup As String
    Dim m_salad As String
    Dim m_pasta As String
    Dim m_desert As String
    Dim m_coffee As String


    Public WriteOnly Property Soup()
        Set(value)
            m_soup = value
        End Set
    End Property

    Public WriteOnly Property Salad()
        Set(value)
            m_salad = value
        End Set
    End Property

    Public WriteOnly Property Pasta()
        Set(value)
            m_pasta = value
        End Set
    End Property

    Public WriteOnly Property Desert()
        Set(value)
            m_desert = value
        End Set
    End Property

    Public WriteOnly Property Coffee()
        Set(value)
            m_coffee = value
        End Set
    End Property



    Public Overrides Function toString() As String
        Return "Soup: " + m_soup + ", Salad: " + m_salad + ", Pasta: " + m_pasta + ", Desert: " + m_desert + ", Coffee: " + m_coffee
    End Function

End Class

MealBuilder Interface
This is the interface that we are going to use our final Meal product , step by step

Public Interface MenuBuilder
    Sub AddSoup()
    Sub AddSalad()
    Sub AddPasta()
    Sub AddDesert()
    Sub AddCoffee()
    Function GetMeal() As Meal
End Interface

KidsMenu Concrete Class
This is concrete class which we used to make the Kids Meal.This class is the implementation of the MenuBuilder interface

Public Class KidsMenu
    Implements MenuBuilder
    Dim m_meal As New Meal


    Public Sub AddCoffee() Implements MenuBuilder.AddCoffee
        m_meal.Coffee = "Kids Coffee"

    End Sub

    Public Sub AddDesert() Implements MenuBuilder.AddDesert
        m_meal.Desert = "Kids Desert"
    End Sub

    Public Sub AddPasta() Implements MenuBuilder.AddPasta
        m_meal.Pasta = "Kids Pasta"
    End Sub

    Public Sub AddSalad() Implements MenuBuilder.AddSalad
        m_meal.Salad = "Kids Salad"
    End Sub

    Public Sub AddSoup() Implements MenuBuilder.AddSoup
        m_meal.Soup = "Kids Soup"
    End Sub

    Public Function GetMeal() As Meal Implements MenuBuilder.GetMeal
        Return m_meal
    End Function


End Class

AdultMenu Concrete Class
This is another concrete class which we use to build adult meal. This class is the implementation of the MenuBuilder interface. We have all methods and properties to build adult meal here

Public Class AdultMenu
    Implements MenuBuilder
    Dim m_meal As New Meal

    Public Sub AddCoffee() Implements MenuBuilder.AddCoffee
        m_meal.Coffee = "Adult Coffee"

    End Sub

    Public Sub AddDesert() Implements MenuBuilder.AddDesert
        m_meal.Desert = "Adult Desert"
    End Sub

    Public Sub AddPasta() Implements MenuBuilder.AddPasta
        m_meal.Pasta = "Adult Pasta"
    End Sub

    Public Sub AddSalad() Implements MenuBuilder.AddSalad
        m_meal.Salad = "Adult Salad"
    End Sub

    Public Sub AddSoup() Implements MenuBuilder.AddSoup
        m_meal.Soup = "Adult Soup"
    End Sub

    Public Function GetMeal() As Meal Implements MenuBuilder.GetMeal
        Return m_meal
    End Function


End Class

MenuDirector Class
Director can use CreateMenu method to build the Menu using MenuBuilder class

Public Class MenuDirector

    Public Function CreateMenu(mb As MenuBuilder) As Meal
        mb.AddSoup()
        mb.AddSalad()
        mb.AddPasta()
        mb.AddDesert()
        mb.AddCoffee()
        Return mb.GetMeal()
    End Function

End Class

This is your Client to build the Menu

Module Module1

    Sub Main()

        Dim adult_menu_builder As MenuBuilder = New AdultMenu()
        Dim menu_director As New MenuDirector()
        Dim adult_meal As Meal = menu_director.createMenu(adult_menu_builder)
        Console.WriteLine(adult_meal)

        Dim kids_menu_builder As MenuBuilder = New KidsMenu()
        Dim kids_meal As Meal = menu_director.createMenu(kids_menu_builder)
        Console.WriteLine(kids_meal)
        Console.ReadLine()

    End Sub

End Module