65.9K
CodeProject is changing. Read more.
Home

Template Method

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jun 13, 2008

CPOL
viewsIcon

9581

Template MethodThe Template Method is know as a behavioral pattern which lets subclasses implement behaviour that can vary. In the example below we

Template Method

The Template Method is know as a behavioral pattern which lets subclasses implement behaviour that can vary. In the example below we use the Template Method to allow sub classes to implement how a hot drink is made.

A VB example of the Temple Method Pattern

Public Class MyPage
          Public Sub Page_Load()
     
        Dim CupOfTea As HotDrink
        CupOfTea = New Tea

        Dim CupOfCoffee As HotDrink
        CupOfCoffee = New Coffee

        MakeDrink(CupOfTea)        

        Response.write(".........
") MakeDrink(CupOfCoffee) End Sub ''' ''' Generic Method that will make the hot Drink ''' Public Sub MakeDrink(ByVal HotDrink As HotDrink) HotDrink.boilWater() HotDrink.addIngredients() HotDrink.addCondiments() End SubEnd Class ''' ''' Abstract Class ''' Public MustInherit Class HotDrink Public Sub boilWater() Response.write("Boiling Water...
") End Sub Public MustOverride Sub addIngredients() Public MustOverride Sub addCondiments()End Class ''' ''' Tea Class that implements behaviour that can vary i.e. ''' the addIngredients & addCondiments methods ''' Public Class Tea Inherits HotDrink Public Overrides Sub addCondiments() Response.write("Add Milk ...
") End Sub Public Overrides Sub addIngredients() Response.write("Add Tea Bags ...
") End SubEnd Class ''' ''' Coffee Class that implements behaviour that can vary i.e. ''' the addIngredients & addCondiments methods ''' Public Class Coffee Inherits HotDrink Public Overrides Sub addCondiments() Response.write("Add Milk ...
") End Sub Public Overrides Sub addIngredients() Response.write("Add Coffee Granules ...
") End Sub End Class

UML

Template Method UML