26.4 combination of the visitor mode and the combination mode
In the visitor mode, an object structure is contained in a set of element objects. We can usually use an iterator to traverse the object structure. At the same time, there can be an overall and partial relationship between specific elements, some elements act as container objects, and some elements act as member objects. elements can be organized in the composite mode. The structure of the visitor mode 26-4 after the combined mode is introduced is shown below:
Note that in the structure shown in Figure 26-4, The traversal of leaf elements has been completed in the container elements, therefore, we should avoid adding the leaf elements that have been added to the container element to the object structure again. Only the container elements and isolated leaf elements are saved in the object structure. 26.5 visitor mode Summary
Because the visitor mode has strict usage conditions and complicated structure, it is not frequently used in practical applications. When a complex object structure exists in the system, and the operations performed by different visitors are not the same, you can consider using the visitor pattern for design. It has been applied to some fields such as XML document parsing, compiler design, and processing of complex set objects.
1. Main advantages
The visitor mode has the following advantages:
(1)
It is convenient to add new access operations. Adding a new access operation in the visitor Mode means adding a new visitor class, which is easy to implement without modifying the source code and complies with the "Open and Close principle ".
(2)
The access behavior of element objects is concentrated in a visitor object, rather than scattered in element classes. Class responsibilities are clearer, which facilitates the reuse of element objects in the object structure. The same object structure can be accessed by multiple different visitors.
(3)
This allows you to define operations that act on the hierarchy without modifying the hierarchy of existing element classes.
2. Main disadvantages
The main disadvantages of the visitor mode are as follows:
(1)
It is very difficult to add new element classes. In the visitor mode, each addition of a new element class means that a new abstract operation should be added to the abstract visitor role, and corresponding specific operations should be added to each specific visitor class, this violates the requirements of the "Open and closed principle.
(2)
Destroys encapsulation. The visitor mode requires the visitor object to access and call the operations of each element object, which means that the element object sometimes must expose some internal operations and internal states, otherwise it cannot be accessed by the visitor.
3. Applicable scenarios
The visitor mode can be considered in the following cases:
(1)
An object structure contains multiple types of objects. You want to perform operations on these objects that depend on their specific types. A visitor provides an access operation for each specific type. Different types of objects can have different access operations.
(2)
You need to perform many different and unrelated operations on the objects in an object structure. Instead, you need to avoid causing these operations to "pollute" the classes of these objects, you do not want to modify these classes when adding new operations. The visitor mode allows us to centralize and define relevant access operations in the visitor class. The object structure can be used by multiple different visitor classes, separates an object from its access operations.
(3)
In the object structure, the class corresponding to the object is rarely changed, but it is often necessary to define new operations on the object structure.
|
Exercise Sunny software company wants to develop a reward approval system for a university. This system can be used to approve teacher rewards and student rewards (award check ), if the number of papers published by a teacher exceeds 10 or the number of student papers exceeds 2, the scientific research award can be selected. If the score of the teacher's teaching feedback is greater than or equal to 90 or the average score of the student is greater than or equal to 90, the award can be selected. Design the system in visitor mode to determine whether the instructors or students in the Candidate Set meet certain award-winning requirements. |
|
[Author: Liu Wei http://blog.csdn.net/lovelion]