Visitor pattern usage scenario and code example of Java design pattern

Java design pattern visitor pattern

Model concept

Visitor mode represents an operation that acts on the elements in an object structure. It allows you to define new operations that act on these elements without changing the element classes. Visitor mode is suitable for systems with relatively stable data structure and easy algorithm change. If the system data structure object is easy to change, visitor mode is not suitable for use. The advantage of visitor mode is that adding operations is easy, because adding operations means adding new visitors.

Visitor application scenario

There must be some questions: the difference between visitor and iterator:

The visitor can access different objects (only need to define the corresponding accept in the element), but the iterator can only access the same object. At least, it must have the same interface. The iterator does not depend on the specific implementation, while the visitor depends on the specific implementation, because the visitor will take the corresponding operation according to the specific object accessed, The iterator is at most a generalized implementation based on the same interface.

The operation of the data structure accessed by the iterator is not separated from the data, so it needs to be modified to expand the function, which violates the opening and closing principle and the principle of single responsibility. However, because visitors rely on concrete implementation rather than abstraction, they violate the dependency inversion principle

Application scenarios determined by advantages and disadvantages

It conforms to the principle of single responsibility and has good expansibility in function. However, it brings trouble for class modification because relying on specific implementation violates specific implementation.

It has excellent expansibility. It only needs to implement a new visitor to meet the new access requirements. Because of the separation of data and operations, it prevents adding new operations from polluting the original data structure.

To sum up

Visitor is a centralized and regular mode, which is especially suitable for large-scale reconstruction projects. The requirements at this stage have been very clear, and the function points of the original system have been clear. Through visitor mode, it is easy to sort out some functions to achieve the ultimate goal of function centralization

Pattern structure

1) Visitor Abstract visitor role: declare an access operation interface for the specific element role in the object structure. The name and parameters of the operation interface identify the specific element role that sends the access request to the specific visitor, so that the visitor can directly access it through the specific interface of the element role.

2) Concretevisitor specific visitor role: implements the interface declared by the visitor.

3) Element Abstract visited element: defines an accept access operation (accept ()), which takes a visitor as a parameter.

4) Concreteelement specific visited element: implements the accept operation interface defined by the abstract element.

5) Objectstructure structure object role: This is a necessary role to use the visitor pattern. It has the following characteristics: it can enumerate its elements; A high-level interface can be provided to allow visitors to access its elements; If necessary, it can be designed as a composite object or an aggregation (such as a list or unordered collection).

Demo

Abstract visitor role:

Specific access roles:

(note) methods with the same name are set in the visitor, and the method parameters are different objects that implement the same interface, i.e. interviewee elements.

Abstract visited element:

Specific interviewed elements:

Structure object role:

Execution process:

Execution results:

Here is a complete code example:

Operation results:

summary

The above is all about the visitor pattern usage scenarios and code examples of Java design patterns in this article. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java programming - consider polymorphism in testing, Java implementation of wechat public platform circle of friends sharing function detailed code, Java programming BigDecimal usage example sharing, etc. if there are deficiencies, please leave a message to point out. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>