(NET) NET (2016)

Composite in VB.NET

A tree structure of simple and composite objects

Return


Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Composite pattern allows the creation of a tree structure in which individual nodes are accessed uniformly whether they are leaf nodes or branch (composite) nodes.







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



   1:      ' Composite Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Composite.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              ' Create a tree structure
   7:              Dim root As New Composite("root")
   8:              root.Add(New Leaf("Leaf A"))
   9:              root.Add(New Leaf("Leaf B"))
  10:              Dim comp As New Composite("Composite X")
  11:              comp.Add(New Leaf("Leaf XA"))
  12:              comp.Add(New Leaf("Leaf XB"))
  13:              root.Add(comp)
  14:              root.Add(New Leaf("Leaf C"))
  15:              ' Add and remove a leaf
  16:              Dim leaf As New Leaf("Leaf D")
  17:              root.Add(leaf)
  18:              root.Remove(leaf)
  19:              ' Recursively display tree
  20:              root.Display(1)
  21:              ' Wait for user
  22:              Console.ReadKey()
  23:          End Sub
  24:      End Class
  25:   
  26:      ' The 'Component' abstract class
  27:      MustInherit Class Component
  28:          Protected name As String
  29:          ' Constructor
  30:          Public Sub New(name As String)
  31:              Me.name = name
  32:          End Sub
  33:          Public MustOverride Sub Add(c As Component)
  34:          Public MustOverride Sub Remove(c As Component)
  35:          Public MustOverride Sub Display(depth As Integer)
  36:      End Class
  37:   
  38:      ' The 'Composite' class
  39:      Class Composite
  40:          Inherits Component
  41:          Private _children As New List(Of Component)()
  42:          ' Constructor
  43:          Public Sub New(name As String)
  44:              MyBase.New(name)
  45:          End Sub
  46:          Public Overrides Sub Add(component As Component)
  47:              _children.Add(component)
  48:          End Sub
  49:          Public Overrides Sub Remove(component As Component)
  50:              _children.Remove(component)
  51:          End Sub
  52:          Public Overrides Sub Display(depth As Integer)
  53:              Console.WriteLine(New [String]("-"c, depth) & name)
  54:              ' Recursively display child nodes
  55:              For Each component As Component In _children
  56:                  component.Display(depth + 2)
  57:              Next
  58:          End Sub
  59:      End Class
  60:   
  61:      ' The 'Leaf' class
  62:      Class Leaf
  63:          Inherits Component
  64:          ' Constructor
  65:          Public Sub New(name As String)
  66:              MyBase.New(name)
  67:          End Sub
  68:          Public Overrides Sub Add(c As Component)
  69:              Console.WriteLine("Cannot add to a leaf")
  70:          End Sub
  71:          Public Overrides Sub Remove(c As Component)
  72:              Console.WriteLine("Cannot remove from a leaf")
  73:          End Sub
  74:          Public Overrides Sub Display(depth As Integer)
  75:              Console.WriteLine(New [String]("-"c, depth) & name)
  76:          End Sub
  77:      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/Composite.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>