The most complex design patterns---visitor patterns

Source: Internet
Author: User

today, let's explore the last and most complex design pattern visitor pattern, which represents an operation that acts on individual elements in an object's structure. It allows you to define new actions that act on these elements without changing the class of elements. This place to note: 1. An object structure class is required to store the method of looping traversal; 2. The visitor pattern is the manipulation of elements; 3. You can add new actions to these elements arbitrarily. It's really a bit complicated, let's look at its structure:

Code section:

The visitor class abstract declares the method of accessing various types of elements;

Abstract class Visitor{public abstract void Visitconcreteelementa (Concreteelementa elementa);p ublic abstract void VISITCONCRETEELEMENTB (Concreteelementb elementb);}

ConcreteVisitor1 and ConcreteVisitor2 respectively define the actual operation of the various elements in two different access states;

Class concretevisitor1:visitor{public override void Visitconcreteelementa (Concreteelementa elementa) { Console.WriteLine ("1 element {0} is accessed by {1}", Elementa.gettype (). Name,this. Gettye (). name);} public override void Visitconcreteelementb (Concreteelementb elementb) {Console.WriteLine ("1 element {0} is accessed by {1}", Elementb.gettype (). Name,this. Gettye (). name);}} Class concretevisitor2:visitor{public override void Visitconcreteelementa (Concreteelementa elementa) { Console.WriteLine ("2 element {0} is accessed by {1}", Elementa.gettype (). Name,this. Gettye (). name);} public override void Visitconcreteelementb (Concreteelementb elementb) {Console.WriteLine ("2 element {0} is accessed by {1}", Elementb.gettype (). Name,this. Gettye (). name);}}

Element is an abstract class of elements, declaring the operation method of an element based on the incoming access state;
Abstract class Element{public abstract void Accept (Visitor Visitor);}
Specific element classes: Concreteelementa, CONCRETEELEMENTB; Specifically, defines the way the element is accessed; This place we are going to mention the two-time dispatch technique. What is two-time allocation? First, the method state is transmitted to the concrete element class in the form of the parameter, and then the method of the incoming access state is passed in as the parameter.

Class concreteelementa:element{public override void Accept (Visitor Visitor) {    //This place has a 2-time allocation technique to separate operations from data structures Visitor . Visitconcreteelementa (this);} Other methods are defined public void Operationa () {}}class concreteelementb:element{public override void Accept (Visitor Visitor) {    This place has a technique of 2 dispatch, which separates the operation from the data structure visitor. VISITCONCRETEELEMENTB (this);} Definition of other methods public void Operationb () {}}


The object structure class is mainly used to collect elements and to access the individual elements after the incoming access state is implemented.

Class Objectstructure{private ilist<element> list=new list<element> ();p ublic void Attach (element Element) {list. ADD (element);} public void Detach (element Element) {list. Delete (element);} public void Accept (Visitor Visitor) {foreach (Element A in list) {a.accept (Visitor);}}}


The client code, first assembles the element, then defines the access state, carries on the access, if we need the new increment operation, only needs to inherit the visitor, and adds two lines of code on the client to be able;

static void Main (string[] arg) {objectstructure o=new objectstructure (); Attach (New Concreteelementa ()); O. Attach (New Concreteelementb ()); ConcreteVisitor1 v1=new ConcreteVisitor1 (); ConcreteVisitor2 v2=new ConcreteVisitor2 ();    O.accept (v1); O. Accept (v2);//If the newly added operation is//concretevisitor3 v3=new ConcreteVisitor3 () as the following code;    O.accept (v3);}
Okay, now think about what time we use this model? In general, when the data structure is relatively stable, we use this pattern to encode, the greatest advantage of this model is the data structure and action on the direct coupling of the operation is reduced; it is convenient for us to add operations to the elements, but this design pattern has drawbacks: the type of data structure elements must be relatively stable.


The most complex design patterns---visitor patterns

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.