(NET) NET (2016)

Decorator in VB.NET

Add responsibilities to objects dynamically

Return


Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Decorator pattern dynamically adds extra functionality to an existing object.







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



   1:      ' Decorator Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Decorator.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              ' Create ConcreteComponent and two Decorators
   7:              Dim c As New ConcreteComponent()
   8:              Dim d1 As New ConcreteDecoratorA()
   9:              Dim d2 As New ConcreteDecoratorB()
  10:              ' Link decorators
  11:              d1.SetComponent(c)
  12:              d2.SetComponent(d1)
  13:              d2.Operation()
  14:              ' Wait for user
  15:              Console.ReadKey()
  16:          End Sub
  17:      End Class
  18:   
  19:      ' The 'Component' abstract class
  20:      MustInherit Class Component
  21:          Public MustOverride Sub Operation()
  22:      End Class
  23:   
  24:      ' The 'ConcreteComponent' class
  25:      Class ConcreteComponent
  26:          Inherits Component
  27:          Public Overrides Sub Operation()
  28:              Console.WriteLine("ConcreteComponent.Operation()")
  29:          End Sub
  30:      End Class
  31:   
  32:      ' The 'Decorator' abstract class
  33:      MustInherit Class Decorator
  34:          Inherits Component
  35:          Protected component As Component
  36:          Public Sub SetComponent(component As Component)
  37:              Me.component = component
  38:          End Sub
  39:          Public Overrides Sub Operation()
  40:              If component IsNot Nothing Then
  41:                  component.Operation()
  42:              End If
  43:          End Sub
  44:      End Class
  45:   
  46:      ' The 'ConcreteDecoratorA' class
  47:      Class ConcreteDecoratorA
  48:          Inherits Decorator
  49:          Public Overrides Sub Operation()
  50:              MyBase.Operation()
  51:              Console.WriteLine("ConcreteDecoratorA.Operation()")
  52:          End Sub
  53:      End Class
  54:   
  55:      ' The 'ConcreteDecoratorB' class
  56:      Class ConcreteDecoratorB
  57:          Inherits Decorator
  58:          Public Overrides Sub Operation()
  59:              MyBase.Operation()
  60:              AddedBehavior()
  61:              Console.WriteLine("ConcreteDecoratorB.Operation()")
  62:          End Sub
  63:          Private Sub AddedBehavior()
  64:          End Sub
  65:      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/Decorator.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>