Behavior mode-Visitor Mode

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. collections;

NamespaceVisitor Mode
{

/*
* Access Mode intent: indicates an operation that acts on each element in an object structure. It can define new operations that act on these elements without changing the classes of each element.
* Key Points of access mode implementation: 1. Use a stable management container to manage the elements to be accessed
* Applicable scenarios of access mode: 1. When there is an effect sealing class level
* 2. perform a variety of different operations on the class level
* 3. These operations follow the class level core functions are orthogonal
* 4. Flexibility for defining new operations in the future
*/

///


/// declares a visitor operation for each subclass of element in the object structure.
//
Public abstract class visitor
{< br> Public abstract void visitconcreteelementa (concreteelementa A);

Public abstract void visitconcreteelementb (concreteelementb);
}

///


/// specific Category
//
Public class concretevisitor1: Visitor
{< br> Public override void visitconcreteelementa (concreteelementa A1)
{< br> /// access data related to element a
}

PublicOverrideVoidVisitconcreteelementb (concreteelementb B1)
{
//Access Data of Element B
}
}

PublicClassConcretevisitor2: visitor
{
PublicOverrideVoidVisitconcreteelementa (concreteelementa A2)
{
//Access specific A2 Element Data
}

Public Override Void Visitconcreteelementb (concreteelementb B2)
{
// Access data related to specific B2 Elements
}
}
/// <Summary>
/// Defines an accept operation, which takes a visitor as the parameter.
/// </Summary>
Public Abstract Class Element
{
Public Abstract Void Accept (visitor );
}

PublicClassConcreteelementa: Element
{
PublicOverrideVoidAccept (visitor)
{
Visitor. visitconcreteelementa (This);

}
PublicVoidOperationa ()
{
//Operations on Element
}
}

Public Class Concreteelementb: Element
{
Public Override Void Accept (visitor)
{
Visitor. visitconcreteelementb ( This );
}
}
/// <Summary>
/// Enumeration of its elements provides a high-level interface to allow visitors to access its elements
/// </Summary>
Public Class Objectstructure
{
Ilist < Element > Elements = New List < Element > ();
Public Void Add (Element E)
{
Elements. Add (E );
}

PublicVoidRemove (Element E)
{
Elements. Remove (E );

}

Public void Accept (visitor)
{< br> foreach (Element E in elements)
{< br> E. accept (visitor);
}< BR >}

Public class appclient
{< br> Public static void main ( string [] ARGs)
{< br> objectstructure OS = New objectstructure ();
OS. add ( New concreteelementa ());
OS. add ( New concreteelementb ());

Concretevisitor1 visitor1=NewConcretevisitor1 ();
Concretevisitor2 visitor2=NewConcretevisitor2 ();

OS. Accept (visitor1 );

OS. Accept (visitor2 );

}

}
}

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.