JAVA design Mode-visitor mode (Visitor) __java

Source: Internet
Author: User

definition: encapsulates operations that act on elements of a data structure, and it can define new operations that function on these elements without altering the data structure.

Type: object behavior pattern

class Diagram:

The

       visitor pattern may be one of the most complex patterns in behavioral patterns, but it's not a reason why we don't have it. Let's start by looking at a simple example of the following code: [Java]   View plain copy class a {        public void method1 () {            System.out.println ("I am a");       }               PUBLIC VOID METHOD2 (b b) {            b.showa (This);       }  }       class b {       public void showa (A a) {            A.METHOD1 ();       }   }  

Let's take a look at the difference between method Method1 and method Method2 in Class A, the method method1 is very simple, is to print out "I am a"; method method2 a little more complex, use Class B as a parameter, and invoke the Showa method of Class B. Take a look at the Showa method of Class B, the Showa method uses Class A as a parameter, and then calls the Method1 method of Class A, you can see that the Method2 method goes around, it is simply to call your own Method1 method, and its running result should also be "I am a", After the analysis, let's run through the two methods and look at the results: [Java] view plain copy public class Test {public static void main (string[] A           RGS) {A A = new A ();           A.method1 ();       A.METHOD2 (New B ()); }   }

The results of the operation are:

I'm A
I'm A

With this example in view, the 90% of the visitor pattern is understood, and in the case of Class A, Class B is a visitor. But this example is not all of the visitor pattern, although intuitive, but its scalability is relatively poor, below we have a general implementation of the visitor pattern, you can see through the class diagram, in the visitor pattern, mainly includes the following roles: abstract Visitors: abstract class or interface, Declare which elements a visitor can access, specifically in a program, the parameters in the Visit method define which objects can be accessed. Visitor: implements the method that the abstract visitor declares, and it affects what the visitor does after accessing a class and what to do. abstract Element class: an interface or abstract class that declares which type of visitor access is accepted and is defined by a parameter in the Accept method. Abstract elements generally have two kinds of methods, one part of their business logic, and the other is to allow the type of visitors to receive access. element class: implements the Accept method declared by the abstract element class, usually visitor.visit (this), which has basically formed a stereotype. Structure object: a container of an element that generally contains a container containing a number of different classes and interfaces, such as list, Set, map, and so on, which is rarely abstracted from the role in the project.

Common code implementation for visitor patterns[Java]  View plain copy abstract class element {       public  Abstract void accept (ivisitor visitor);       public abstract  void dosomething ();  }      interface ivisitor {       public void visit (concreteelement1 el1);        public void visit (CONCRETEELEMENT2 EL2);  }      class  concreteelement1 extends element {       public void  DoSomething () {           system.out.println ("This is Element 1");        }              public  void accept (ivisitor visitor)  {            vIsitor.visit (This);       }  }      class  concreteelement2 

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.