Design Pattern-Visitor pattern (visitor)

Source: Internet
Author: User

 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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.