(NET) NET (2016)

Strategy in VB.NET

Encapsulates an algorithm inside a class

Return


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Strategy pattern encapsulate functionality in the form of an object. This allows clients to dynamically change algorithmic strategies.







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



   1:      ' Strategy Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Strategy.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              Dim context As Context
   7:              ' Three contexts following different strategies
   8:              context = New Context(New ConcreteStrategyA())
   9:              context.ContextInterface()
  10:              context = New Context(New ConcreteStrategyB())
  11:              context.ContextInterface()
  12:              context = New Context(New ConcreteStrategyC())
  13:              context.ContextInterface()
  14:              ' Wait for user
  15:              Console.ReadKey()
  16:          End Sub
  17:      End Class
  18:   
  19:      ' The 'Strategy' abstract class
  20:      MustInherit Class Strategy
  21:          Public MustOverride Sub AlgorithmInterface()
  22:      End Class
  23:   
  24:      ' A 'ConcreteStrategy' class
  25:      Class ConcreteStrategyA
  26:          Inherits Strategy
  27:          Public Overrides Sub AlgorithmInterface()
  28:              Console.WriteLine("Called ConcreteStrategyA.AlgorithmInterface()")
  29:          End Sub
  30:      End Class
  31:   
  32:      ' A 'ConcreteStrategy' class
  33:      Class ConcreteStrategyB
  34:          Inherits Strategy
  35:          Public Overrides Sub AlgorithmInterface()
  36:              Console.WriteLine("Called ConcreteStrategyB.AlgorithmInterface()")
  37:          End Sub
  38:      End Class
  39:   
  40:      ' A 'ConcreteStrategy' class
  41:      Class ConcreteStrategyC
  42:          Inherits Strategy
  43:          Public Overrides Sub AlgorithmInterface()
  44:              Console.WriteLine("Called ConcreteStrategyC.AlgorithmInterface()")
  45:          End Sub
  46:      End Class
  47:   
  48:      ' The 'Context' class
  49:      Class Context
  50:          Private _strategy As Strategy
  51:          ' Constructor
  52:          Public Sub New(strategy As Strategy)
  53:              Me._strategy = strategy
  54:          End Sub
  55:          Public Sub ContextInterface()
  56:              _strategy.AlgorithmInterface()
  57:          End Sub
  58:      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/Strategy.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>