Composite in VB.NET
A tree structure of simple and composite objects
ReturnCompose 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
- Abstract Factory in VB.NET - Creates an instance of several families of classes.
- Builder in VB.NET - Separates object construction from its representation.
- Factory Method in VB.NET - Creates an instance of several derived classes.
- Prototype in VB.NET - A fully initialized instance to be copied or cloned.
- Singleton in VB.NET - A class of which only a single instance can exist.
- Adapter in VB.NET - Match interfaces of different classes
- Bridge in VB.NET - Separates an object???s interface from its implementation
- Composite in VB.NET - A tree structure of simple and composite objects
- Decorator in VB.NET - Add responsibilities to objects dynamically
- Facade in VB.NET - A single class that represents an entire subsystem
- Flyweight in VB.NET - A fine-grained instance used for efficient sharing
- Proxy in VB.NET - An object representing another object
- Chain of Resp. in VB.NET - A way of passing a request between a chain of objects
- Command in VB.NET - Encapsulate a command request as an object
- Interpreter in VB.NET - A way to include language elements in a program
- Iterator in VB.NET - Sequentially access the elements of a collection
- Mediator in VB.NET - Defines simplified communication between classes
- Memento in VB.NET - Capture and restore an object's internal state
- Observer in VB.NET - A way of notifying change to a number of classes
- State in VB.NET - Alter an object's behavior when its state changes
- Strategy in VB.NET - Encapsulates an algorithm inside a class
- Template Method in VB.NET - Defer the exact steps of an algorithm to a subclass
- Visitor in VB.NET - Defines a new operation to a class without change
Comments (
)
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> |