Visitor Mode(Visitor)
Definition
Indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.
Motivation
The visitor mode is suitable for systems with relatively stable data structures. It frees the coupling between the data structure and the operations acting on the structure, allowing the operation set to evolve relatively freely. The goal is to separate processing from the data structure.
Visitor Mode(Visitor)
Structure chart
Visitor class, which declares a visit operation for each class of concreteelement in the object structure.
Abstract class visitor
{
Public abstract void visitconcreteelementa (concreteelementa );
Public abstract void visitconcreteelementb (concreteelementa concreteelementb );
}
Concretevisitor1 and concretevisitor1. Specific visitors implement each operation declared by the visitor. Each operation is part of an algorithm, and the algorithm segment is a class corresponding to objects in the structure.
Class concretevisitor1: visitor
{
Public override void visitconcreteelementa (concreteelementa)
{
Console. writeline ("{0} accessed by {1}", concreteelementa. GetType (). Name, this. GetType (). Name );
}
Public override void visitconcreteelementb (concreteelementb)
{
Console. writeline ("{0} accessed by {1}", concreteelementb. GetType (). Name, this. GetType (). Name );
}
}
Class concretevisitor2: visitor
{
Public override void visitconcreteelementa (concreteelementa)
{
Console. writeline ("{0} accessed by {1}", concreteelementa. GetType (). Name, this. GetType (). Name );
}
Public override void visitconcreteelementb (concreteelementb)
{
Console. writeline ("{0} accessed by {1}", concreteelementb. GetType (). Name, this. GetType (). Name );
}
}
The element class defines an accept operation, which takes a visitor as the parameter.
Abstract class Element
{
Public abstract void accept (visitor );
}
Concreteelementa and concreteelementb classes, specific elements, accept operation.
Class concreteelementa: Element
{
Public override void accept (visitor)
{
Visitor. visitconcreteelementa (this );
}
Public void operation ()
{
}
}
Class concreteelementb: Element
{
Public override void accept (visitor)
{
Visitor. visitconcreteelementb (this );
}
Public void operation ()
{
}
}
Objectstructure class, which can enumerate its elements. It can provide a high-level interface to allow visitors to access its elements.
Class objectstructure
{
Private ilist <element> elements = new list <element> ();
Public void attach (element)
{
Elements. Add (element );
}
Public void detach (element)
{
Elements. Remove (element );
}
Public void accept (visitor)
{
Foreach (Element E in elements)
{
E. Accept (visitor );
}
}
}
Client code:
Static void main (string [] ARGs)
{
Objectstructure o = new objectstructure ();
O. Attach (New concreteelementa ());
O. Attach (New concreteelementb ());
Concretevisitor1 V1 = new concretevisitor1 ();
Concretevisitor2 v2 = new concretevisitor2 ();
O. Accept (V1 );
O. Accept (V2 );
}