Design mode Visitor

zhaozj2021-02-16  113

Visitor defines the operation of each object that acts in a part group. It allows you to define new operations for these objects without changing these objects themselves.

In Java, Visitor mode is actually an element that separates the Collection structure and operates these elements.

Why use Visitor? Java's Collection (including Vector and Hashtable) is our most frequently used technology, but the Collection seems to be a black large dyeing tank. Once the objects of various types of characteristics have been put out, these types are removed. Disappeared. Then we must 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 above example, we used instanceof to determine the type of O.

Obviously, the shortcomings of the shortcomings if else IF is very cumbersome. We can use the Visitor mode to solve it.

How to use Visitor? For the above example, we design a visitor visitors:

Public interface visitor {public void visitcollection; public void visitstring (String string); public void visitfloat (float float);}

In this interface, we think that Collection is possible to place the type of possible category.

With visitors, we need to be visited, visitors are each element Element of our Collection, we have to define a interface that can be accessed (access and access to the interviews, only visitors) If you don't welcome you, visitors can't access it),

We define this interception Visitable to define an Accept operation, that is, let Collection each element has accessibility.

Public interface visitable {public void accept (visitor visitor);}

Ok, there are two interfaces, we have to define their specific implementation (Concrete Class):

public class ConcreteElement implements Visitable {private String value; public ConcreteElement (String string) {value = string;} // accept specific content defined herein is a simple call public void accept (Visitor visitor) {visitor.visitString (this) }}

Look at the visitor's Concrete implementation:

public class ConcreteVisitor implements Visitor {// In this method, we have achieved success in the Collection of the elements of access public void visitCollection (Collection collection) {Iterator iterator = collection.iterator () while (iterator.hasNext ()) {Object o = iTerator.next (); if (visitable o) .accept (this);} public void visitstring (String string) {system.out.println ("'" string ""); } Public void visitfloat (float float) {system.out.println (float.tostring () "f");}}? Only VisitCollection we implemented each element of Collection, only one judgment statement, Just determine if it can be accessed.

At this point, we completed the Visitor mode basic architecture.

Using the Visitor mode, the item type in the item group is rarely changed, that is, the visitor's identity type is rare, and if the type in Visitor in the above changes, if you need to add new operations, For example, in the above example we need new ConcreteElement2 ConcreteElement3 in the concretelement specific implementation.

It can be seen that using the Visitor mode is premising, in two interface Visitor and Visitable, make sure that Visitor rarely changes, Visitable, which is most convenient to use Visitor.

If Visitor changes, it is, that is, the type of items in the object population changes, and general recommendations are not as defined by one of these object categories. But Java's Reflect technology solves this problem.

Reflect Technology is a technique that dynamically acquires item types and methods during operation.

转载请注明原文地址:https://www.9cbs.com/read-11468.html

New Post(0)