Design Pattern pocket edition-contact reprint visitor

Source: Internet
Author: User

Visitor Definition

Operations on objects in an object group. This allows you to define new operations on these objects without changing the objects themselves.

In Java, the visitor mode separates elements in the collection structure from the operations on these elements.

Why use visitor?
Java collections (including vector and hashtable) are the most frequently used technologies. However, collection seems to be a large black dye cylinder. Once an object with various distinctive features is put in, then, these types disappear. therefore, we must use if to judge, for example:

Program code:

Iterator = collection. iterator ()
While (iterator. hasnext ()){
Object o = iterator. Next ();
If (O instanceof collection)
Messyprintcollection (Collection) O );
Else if (O instanceof string)
System. Out. println ("'" + O. tostring () + "'");
Else if (O instanceof float)
System. Out. println (O. tostring () + "F ");
System. Out. println (O. tostring ());

In the above example, we use instanceof to determine the O type.

Obviously, the disadvantage of doing so is that the Code if else if is cumbersome. We can use the visitor mode to solve it.

How to Use visitor?
For the above example, the defined interface is called visitable, which is used to define an accept operation, that is, to make each element of the collection accessible.

Visitors are each element of our collection. We need to define an interface for these elements that can be accessed (access and access are interactive, only visitors, if the visitor says they are not welcome, the visitor will not be able to access it ),
Program code:

Public interface visitable
Public void accept (visitor );

We designed an interface for visitor Visitor:
Program code:

Public interface visitor
Public void visitcollection (Collection collection );
Public void visitstring (stringelement stringe );


Well, with these two interfaces, we need to define their specific implementation (concrete class ):
Program code:

Public class stringelement implements visitable
Private string value;
Public stringelement (string ){
Value = string;

Public String getvalue (){
Return value;

// Define the accept content. Here is a simple call.
Public void accept (visitor ){
Visitor. visitstring (this );


In this interface, we put the types of classes that we think are possible for collection.

With the visitor, we need the visitor's concrete implementation:
Program code:

Public class concretevisitor implements visitor
// In this method, we have successfully accessed the elements of the collection.
Public void visitcollection (Collection collection ){
Iterator = collection. iterator ()
While (iterator. hasnext ()){
Object o = iterator. Next ();
If (O instanceof visitable)
(Visitable) O). Accept (this );

Public void visitstring (visitable string ){
System. Out. println ("'" + String. getvalue () + "'");



In the preceding visitcollection, we implemented access to each element of the collection. We only used one judgment statement to determine whether it can be accessed.

Stringelement is only an implementation that can be extended to more implementations. The entire core mystery is that in the accept method, when traversing the collection, the corresponding accept method is used to call specific types of visitors. In this step, the types of visitors are determined,

If it is stringelement, and stringelement calls back the visitor's visitestring method, this step implements the behavior operation method.

So far, we have completed the basic architecture of the visitor mode.

Prerequisites for using the visitor Mode
In the object group structure (collection), the object type is rarely changed, that is, the visitor's identity type is rarely changed. For example, the type in the visitor is rarely changed. If you need to add a new operation, for example, in the above example, we need a new concreteelement2 concreteelement3.

It can be seen that there is a premise to use the visitor mode. In the visitor and visitable interfaces, the visitable mode ensures that the visitor is rarely changed, and the visitable mode is the most convenient to use.

If the visitor changes frequently, that is, the object type in the object group changes frequently, it is recommended that you define operations one by one in these object classes. However, the reflect Technology of Java solves this problem.

Reflect is a technique used to dynamically obtain object types and methods during running. For more information, see the original javaworld document.

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: 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.