(NET) NET (2016)

Command in VB.NET

Encapsulate a command request as an object

Return


Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Command pattern which stores requests as objects allow clients to execute or playback the requests.







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



   1:      ' Command Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Command.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              ' Create receiver, command, and invoker
   7:              Dim receiver As New Receiver()
   8:              Dim command As Command = New ConcreteCommand(receiver)
   9:              Dim invoker As New Invoker()
  10:              ' Set and execute command
  11:              invoker.SetCommand(command)
  12:              invoker.ExecuteCommand()
  13:              ' Wait for user
  14:              Console.ReadKey()
  15:          End Sub
  16:      End Class
  17:   
  18:      ' The 'Command' abstract class
  19:      MustInherit Class Command
  20:          Protected receiver As Receiver
  21:          ' Constructor
  22:          Public Sub New(receiver As Receiver)
  23:              Me.receiver = receiver
  24:          End Sub
  25:          Public MustOverride Sub Execute()
  26:      End Class
  27:   
  28:      ' The 'ConcreteCommand' class
  29:      Class ConcreteCommand
  30:          Inherits Command
  31:          ' Constructor
  32:          Public Sub New(receiver As Receiver)
  33:              MyBase.New(receiver)
  34:          End Sub
  35:          Public Overrides Sub Execute()
  36:              receiver.Action()
  37:          End Sub
  38:      End Class
  39:   
  40:      ' The 'Receiver' class
  41:      Class Receiver
  42:          Public Sub Action()
  43:              Console.WriteLine("Called Receiver.Action()")
  44:          End Sub
  45:      End Class
  46:   
  47:      ' The 'Invoker' class
  48:      Class Invoker
  49:          Private _command As Command
  50:          Public Sub SetCommand(command As Command)
  51:              Me._command = command
  52:          End Sub
  53:          Public Sub ExecuteCommand()
  54:              _command.Execute()
  55:          End Sub
  56:      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/Command.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>