(NET) NET (2016)

Template Method in VB.NET

Defer the exact steps of an algorithm to a subclass

Return


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. Template method provide a skeleton calling sequence of methods. One or more steps can be deferred to subclasses which implement these steps without changing the overall calling sequence.







Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern



   1:      ' Template Method Design Pattern. 
   2:      ' See description in //www.vb-net.com/ProgramTheory/TemplateMethod.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              Dim aA As AbstractClass = New ConcreteClassA()
   7:              aA.TemplateMethod()
   8:              Dim aB As AbstractClass = New ConcreteClassB()
   9:              aB.TemplateMethod()
  10:              ' Wait for user
  11:              Console.ReadKey()
  12:          End Sub
  13:      End Class
  14:   
  15:      ' The 'AbstractClass' abstract class
  16:      MustInherit Class AbstractClass
  17:          Public MustOverride Sub PrimitiveOperation1()
  18:          Public MustOverride Sub PrimitiveOperation2()
  19:          ' The "Template method"
  20:          Public Sub TemplateMethod()
  21:              PrimitiveOperation1()
  22:              PrimitiveOperation2()
  23:              Console.WriteLine("")
  24:          End Sub
  25:      End Class
  26:   
  27:      ' A 'ConcreteClass' class
  28:      Class ConcreteClassA
  29:          Inherits AbstractClass
  30:          Public Overrides Sub PrimitiveOperation1()
  31:              Console.WriteLine("ConcreteClassA.PrimitiveOperation1()")
  32:          End Sub
  33:          Public Overrides Sub PrimitiveOperation2()
  34:              Console.WriteLine("ConcreteClassA.PrimitiveOperation2()")
  35:          End Sub
  36:      End Class
  37:   
  38:      ' A 'ConcreteClass' class
  39:      Class ConcreteClassB
  40:          Inherits AbstractClass
  41:          Public Overrides Sub PrimitiveOperation1()
  42:              Console.WriteLine("ConcreteClassB.PrimitiveOperation1()")
  43:          End Sub
  44:          Public Overrides Sub PrimitiveOperation2()
  45:              Console.WriteLine("ConcreteClassB.PrimitiveOperation2()")
  46:          End Sub
  47:      End Class




See also:
Creational Patterns Structural Patterns Behavioral Patterns

Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/ProgramTheory/TemplateMethod.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>