Visitor mode of Java design pattern

Source: Internet
Author: User

Visitor definition

Actions that act on objects in an object group. It allows you to define new actions that work on these objects without changing the objects themselves.

In Java, the visitor pattern is actually a separation of elements in the collection structure and the act of manipulating those elements.

Why use visitor?

Java collection (including Vector and Hashtable) is the technology we use most often, but collection is like a black big dyeing tank, when objects with distinct types of features are put in and removed, these types disappear. Then we are bound to use if to judge, such as:

Iterator 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");
   else
      System.out.println(o.toString());
}

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

Obviously, the disadvantage of doing this code if else if is cumbersome. We can use the visitor mode to solve it.

How to use visitor?/

For the above example, we design an interface visitor visitor:

public interface Visitor
{
   public void visitCollection(Collection collection);
   public void visitString(String string);
   public void visitFloat(Float float);
}

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

With the visitor, we need to be the visitor, the visitor is each element of our collection, we want to define an accessible interface for these elements (access and access is interactive, only visitors, if the visitors are not welcome, visitors can not access )。

We define this interface called Visitable, which is used to define a accept operation, which means that collection each element is accessible.

public interface Visitable
{
   public void accept(Visitor visitor);
}

Well, with two interfaces, we're going to define their specific implementations (concrete Class):

public class ConcreteElement implements Visitable
{
   private String value;
   public ConcreteElement(String string) {
      value = string;
   }
   //定义accept的具体内容 这里是很简单的一句调用
   public void accept(Visitor visitor) {
      visitor.visitString(this);
   }
}

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.