Template Design Pattern in Visual Basic .NET

September 7, 2017

Definition

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

I will explain this design pattern with an example later

This is the UML diagram for Template Design Pattern

Participant

AbstractClass
This abstract class define methods that concrete call can implement
If there are steps in algorithm that are not going to change you can write those steps here
ConcreteClass
Concrete class can overrides the methods in abstract class. That means you refine the step in algorithm here

Template Design Pattern example in VB.NET

It is better to understand this Template Design Pattern with an example. Lets say you have a tour class. To make a tour you have to do some steps like starting tour, travel method and end tour
So the skeleton of tour algorithm to make tour is
-StartTour()
-Travel()
-EndTour()

You can do the travel by car,bike or bus. So you have to write methods separately for travel(). But StartTour() and EndTour() will have same pattern. In other worlds , you have to redefine the Travel() for each cases

Now lets draw the UML diagram for above example using Template Design Pattern

Now you can start writing the code in VB.NET
First create the Tour abstract class

Public MustInherit Class Tour
 
    Sub StartTour()
        Console.WriteLine("Tour Started")
    End Sub
    MustOverride Sub Travel()
    MustOverride Sub EndTour()

    Public Sub MakeTour()
        StartTour()
        Travel()
        EndTour()
    End Sub

End Class

Next you can create the Concrete classes for travel methods
TtravelByCar Class

Public Class TravelByCar
    Inherits Tour

    Public Overrides Sub Travel()
        Console.WriteLine("I am traveling by car now")
    End Sub

    Public Overrides Sub EndTour()
        Console.WriteLine("My travel by car tour ended")
    End Sub

   
End Class

TravelByBike Class

Public Class TravelByBike
    Inherits Tour

    Public Overrides Sub Travel()
        Console.WriteLine("I am traveling by bike now")
    End Sub

    Public Overrides Sub EndTour()
        Console.WriteLine("My travel by bike tour ended")
    End Sub

  
End Class

TravelByBus Class

Public Class TravelByBus
    Inherits Tour

    Public Overrides Sub Travel()
        Console.WriteLine("I am traveling by bus now")
    End Sub

    Public Overrides Sub EndTour()
        Console.WriteLine("My travel by bus tour ended")
    End Sub

End Class

Now we will write the client code to run above pattern

Module Module1

    Sub Main()
        Dim myTour As Tour = New TravelByCar()
        myTour.MakeTour()
        myTour = New TravelByBike()
        myTour.MakeTour()

        Console.ReadLine()
    End Sub

End Module

finally,Your output should be like this

Tour Started
I am traveling by car now
My travel by car tour ended
Tour Started
I am traveling by bike now
My travel by bike tour ended