(NET) NET (2016)

State in VB.NET

Alter an object's behavior when its state changes

Return


Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. State pattern allows an object to behave differently depending on its internal state. The difference in behavior is delegated to objects that represent this state.







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



   1:      ' State Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/State.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              ' Setup context in a state
   7:              Dim c As New Context(New ConcreteStateA())
   8:              ' Issue requests, which toggles state
   9:              c.Request()
  10:              c.Request()
  11:              c.Request()
  12:              c.Request()
  13:              ' Wait for user
  14:              Console.ReadKey()
  15:          End Sub
  16:      End Class
  17:   
  18:      ' The 'State' abstract class
  19:      MustInherit Class State
  20:          Public MustOverride Sub Handle(context As Context)
  21:      End Class
  22:   
  23:      ' A 'ConcreteState' class
  24:      Class ConcreteStateA
  25:          Inherits State
  26:          Public Overrides Sub Handle(context As Context)
  27:              context.State = New ConcreteStateB()
  28:          End Sub
  29:      End Class
  30:   
  31:      ' A 'ConcreteState' class
  32:      Class ConcreteStateB
  33:          Inherits State
  34:          Public Overrides Sub Handle(context As Context)
  35:              context.State = New ConcreteStateA()
  36:          End Sub
  37:      End Class
  38:   
  39:      ' The 'Context' class
  40:      Class Context
  41:          Private _state As State
  42:          ' Constructor
  43:          Public Sub New(state As State)
  44:              Me.State = state
  45:          End Sub
  46:          ' Gets or sets the state
  47:          Public Property State() As State
  48:              Get
  49:                  Return _state
  50:              End Get
  51:              Set(value As State)
  52:                  _state = value
  53:                  Console.WriteLine("State: " + _state.[GetType]().Name)
  54:              End Set
  55:          End Property
  56:          Public Sub Request()
  57:              _state.Handle(Me)
  58:          End Sub
  59:      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/State.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>