Visitor in VB.NET
Defines a new operation to a class without change
ReturnRepresent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Different visitor objects define different operations.
Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern
1: ' Visitor Design Pattern.
2: ' See description in //www.vb-net.com/ProgramTheory/Visitor.htm
3: Class MainApp
4: Public Shared Sub Main()
5: ' Setup structure
6: Dim o As New ObjectStructure()
7: o.Attach(New ConcreteElementA())
8: o.Attach(New ConcreteElementB())
9: ' Create visitor objects
10: Dim v1 As New ConcreteVisitor1()
11: Dim v2 As New ConcreteVisitor2()
12: ' Structure accepting visitors
13: o.Accept(v1)
14: o.Accept(v2)
15: ' Wait for user
16: Console.ReadKey()
17: End Sub
18: End Class
19:
20: ' The 'Visitor' abstract class
21: MustInherit Class Visitor
22: Public MustOverride Sub VisitConcreteElementA(concreteElementA As ConcreteElementA)
23: Public MustOverride Sub VisitConcreteElementB(concreteElementB As ConcreteElementB)
24: End Class
25:
26: ' A 'ConcreteVisitor' class
27: Class ConcreteVisitor1
28: Inherits Visitor
29: Public Overrides Sub VisitConcreteElementA(concreteElementA As ConcreteElementA)
30: Console.WriteLine("{0} visited by {1}", concreteElementA.[GetType]().Name, Me.[GetType]().Name)
31: End Sub
32: Public Overrides Sub VisitConcreteElementB(concreteElementB As ConcreteElementB)
33: Console.WriteLine("{0} visited by {1}", concreteElementB.[GetType]().Name, Me.[GetType]().Name)
34: End Sub
35: End Class
36:
37: ' A 'ConcreteVisitor' class
38: Class ConcreteVisitor2
39: Inherits Visitor
40: Public Overrides Sub VisitConcreteElementA(concreteElementA As ConcreteElementA)
41: Console.WriteLine("{0} visited by {1}", concreteElementA.[GetType]().Name, Me.[GetType]().Name)
42: End Sub
43: Public Overrides Sub VisitConcreteElementB(concreteElementB As ConcreteElementB)
44: Console.WriteLine("{0} visited by {1}", concreteElementB.[GetType]().Name, Me.[GetType]().Name)
45: End Sub
46: End Class
47:
48: ' The 'Element' abstract class
49: MustInherit Class Element
50: Public MustOverride Sub Accept(visitor As Visitor)
51: End Class
52:
53: ' A 'ConcreteElement' class
54: Class ConcreteElementA
55: Inherits Element
56: Public Overrides Sub Accept(visitor As Visitor)
57: visitor.VisitConcreteElementA(Me)
58: End Sub
59: Public Sub OperationA()
60: End Sub
61: End Class
62:
63: ' A 'ConcreteElement' class
64: Class ConcreteElementB
65: Inherits Element
66: Public Overrides Sub Accept(visitor As Visitor)
67: visitor.VisitConcreteElementB(Me)
68: End Sub
69: Public Sub OperationB()
70: End Sub
71: End Class
72:
73: ' The 'ObjectStructure' class
74: Class ObjectStructure
75: Private _elements As New List(Of Element)()
76: Public Sub Attach(element As Element)
77: _elements.Add(element)
78: End Sub
79: Public Sub Detach(element As Element)
80: _elements.Remove(element)
81: End Sub
82: Public Sub Accept(visitor As Visitor)
83: For Each element As Element In _elements
84: element.Accept(visitor)
85: Next
86: End Sub
87: 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/Visitor.htm
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |