Complex object structure operations-Visitor mode (2)

Source: Internet
Author: User
26.2 overview of visitor Mode

The visitor mode is a complex behavior design mode. It contains two main components: the visitor and the accessed element. These accessed elements usually have different types, different visitors can perform different access operations on them. For example, the Drug Information in the prescription form is the element to be accessed, and the price-based personnel and pharmacy staff are visitors. The visitor mode allows users to expand the system functions without modifying the existing system, and add new operations for these different types of elements.

When the visitor mode is used, the accessed elements do not exist independently. They are stored in a collection, which is called an "object structure ", visitors can traverse the object structure to perform operations on the stored elements one by one.

The visitor mode is defined as follows:

Visitor pattern: provides an operation representation that acts on each element in an object structure, it allows us to define new operations acting on these elements without changing the classes of each element. The visitor mode is an object behavior mode.

The structure of the visitor mode is complex, as shown in Figure 26-2:

The visitor mode structure includes the following roles:

● Vistor (Abstract visitor): the abstract visitor declares an access operation for each specific element class concreteelement in the object structure, from the operation name or parameter type, you can clearly know the type of the specific element to be accessed. Specific visitors need to implement these operation methods and define access operations on these elements.

● Concretevisitor (Specific visitor): a specific visitor implements each operation declared by an abstract visitor. Each operation is used to access one type of element in the object structure.

● Element (abstract element): An abstract element is generally an abstract class or interface. It defines an accept () method. This method usually uses an abstract visitor as a parameter. [Why is this design introduced later .]

● Concreteelement (specific element): The specific element implements the accept () method, and calls the visitor's access method in the accept () method to complete operations on an element.

● Objectstructure: an object structure is a collection of elements. It is used to store element objects and provides methods to traverse internal elements. It can be implemented in combination with the combination mode, or it can be a simple set object, such as a list object or a set object.

In visitor mode, the object structure stores different types of element objects for different visitors to access. The visitor mode consists of two hierarchies: Visitor hierarchies, which provide abstract visitors and specific visitors, and element hierarchies, which provide abstract elements and specific elements. The same visitor can access different elements in different ways, and the same element can be accessed by different visitors in different ways. In the visitor mode, new visitors do not need to modify the original system, and the system has good scalability.

In the visitor mode, abstract visitors define methods for accessing element objects. Generally, an access method is provided for each type of element object, which can be implemented by specific visitors. There are two methods to name these access methods: one is to directly indicate the specific type of the element object to be accessed in the method name, such as visitelementa (elementa ), another method is to define a series of overloaded visit () methods by using different parameter types. Of course, if all visitors have the same access operations on a certain type of elements, you can move the operation code to the abstract visitor class. The typical code is as follows:

Abstract class visitor {public abstract void visit (concreteelementa elementa); public abstract void visit (incluelementb); Public void visit (concreteelementc elementc) {// The concreteelementc operation code }}

Here, the visit () method is used to define multiple methods for operating different types of element objects. Abstract access methods are implemented in the concretevisitor subclass of the abstract visitor class to define operations on different types of element objects. The typical code of the visitor class is as follows:

Class concretevisitor extends visitor {public void visit (concreteelementa elementa) {// element concreteelementa operation code} public void visit (concreteelementb elementb) {// element concreteelementb operation code }}

For element classes, an accept () method is generally defined to accept access from visitors. The typical abstract element class code is as follows:

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

It should be noted that this method has passed in an abstract visitor type parameter, that isProgramming abstract visitors, not specific visitorsWhen the program is running, determine the type of the visitor and call the visit () method of the visitor object to perform operations on the element object. The accept () method is implemented in the subclass of the abstract element class element to accept access from visitors. In the specific element class, you can also define the unique business methods of different types of elements, the typical code is as follows:

Class concreteelementa implements element {public void accept (visitor) {visitor. Visit (this) ;}public void operationa () {// business method }}

In the accept () method of a specific element class concreteelementa, you can access the element by calling the visit () method of the visitor class, and use the current object as the parameter of the visit () method. The specific execution process is as follows:

(1)
Call the accept (visitor) method of the specific element class, andTake the visitor subclass object as its parameter;

(2)
Call the visit () method of the input visitor object within the accept (visitor) method of the specific element class, such as visit (concreteelementa
Elementa ),Use the current specific element Class Object (this) as the parameter, Such as visitor. Visit (this );

(3)
Execute the visit () method of the visitor object and call the Business Method of the specific element object.

This call mechanism is also called"Double dispatchBecause of the dual allocation mechanism, adding new visitors does not need to modify the existing class library code. Instead, you only need to pass the new visitor object as a parameter to the accept () method of the specific element object, when the program is running, the system calls back the visit () method defined in the new visitor class to add an element access method.

Thoughts

How can I implement the dual dispatch mechanism using code?

In the visitor mode, an object structure is a set used to store element objects and receive access from visitors. The typical code is as follows:

Class objectstructure {private arraylist <element> List = new arraylist <element> (); // defines a set to store the Element Object public void accept (visitor) {iterator I = List. iterator (); While (I. hasnext () {(element) I. next ()). accept (visitor); // traverses every element in the access set} public void addelement (element) {list. add (element);} public void removeelement (element) {list. remove (element );}}

In the object structure, you can use the iterator to traverse the element objects stored in the Set, and call the accept () method of each object one by one to access the element objects.

Thoughts

Does the visitor mode comply with the "open and closed principle "? [Consider adding new visitors and adding new elements .]

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.