Template Design Pattern in Visual Basic .NET


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


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
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

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

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

TravelByBike Class

TravelByBus Class

Now we will write the client code to run above pattern

finally,Your output should be like this

Leave a Reply

Your email address will not be published. Required fields are marked *