Design Mode Study Notes-Visitor Mode

Source: Internet
Author: User

Overview:

The visitor pattern (visitor), which indicates operations on each element in an object structure.

It allows you to define new operations on these elements without changing the classes of each element.

Applicable scenarios:

1. An object structure contains many class objects with different interfaces. You want to perform operations on these objects dependent on their specific classes.

2. You need to perform many different and unrelated operations on the objects in an object, and you want to avoid 'polluting 'the classes of these objects.

Visitor allows you to define related operations in a class.

When this object structure is shared by many applications, use the visitor mode to include the required operations for each application.

3. The definition object structure class rarely changes, but it is often necessary to define new operations on this structure.

Changing the object structure class requires redefining the interfaces for all visitors, which may be costly.

If the object structure class changes frequently, it may be better to define these operations in these classes.

Class diagram:

CodeExample:

1. Visitor abstract class

   ///     <Summary>  
/// Declares a visit operation for each class of concreteelement in the object structure.
/// </Summary>
Abstract Class Visitor
{
Public Abstract Void Visitconcreteelementa (concreteelementa );

Public Abstract Void Visitconcreteelementb (concreteelementb );
}

2. Implement the visitor class

   ///     <Summary>  
/// Visitor to implement the visitor operation. Each operation is implementedAlgorithmThe algorithm corresponds
/// Class of the object in the Structure
/// </Summary>
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 );
}
}

3. Define the element, which takes a visitor as the parameter

   ///        
/// define an accept operation, it uses a visitor as the parameter
///
abstract class element
{< br> Public abstract void Accept (visitor);
}

4. Implement the accept operation

   ///     <Summary>  
/// Specific elements for accept operations
/// </Summary>
Class Concreteelementa: Element
{
/// <Summary>
/// Fully utilizes the dual-allocation technology to separate processing from Data Structures
/// </Summary>
/// <Param name = "visitor"> </param>
Public Override Void Accept (visitor)
{
Visitor. visitconcreteelementa ( This );
}
/// <Summary>
/// Other related methods
/// </Summary>
Public Void Operationa ()
{
}
}
Class Concreteelementb: Element
{
Public Override Void Accept (visitor)
{
Visitor. visitconcreteelementb ( This );
}
Public Void Operationb (){}
}

5. Define a high-level interface

   ///     <Summary>  
/// Provides a high-level interface to allow visitors to access its elements
/// </Summary>
Class Objectstructure
{
Private Ilist < Element > Elements = New List < Element > ();

Public Void Attach (element)
{
Elements. Add (element );
}
Public Void Dettach (element)
{
Elements. Remove (element );
}
Public Void Accept (visitor)
{
Foreach (Element E In Elements)
{
E. Accept (visitor );
}
}
}

6. Client call

   ///     <Summary>  
/// Test visitor Mode
/// </Summary>
Static Void Testvisitor ()
{
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 );

Console. Read ();
}

Summary:

The visitor mode is a bit complex and rarely used. Specific applications need to reasonably abstract the actual situation, and multiple design modes are required to work together.

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.