In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to deeply understand the visitor pattern in the Java design pattern, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. what is the Visitor pattern
Definition: represents an operation that acts on elements in its object structure, allowing you to define new operations that act on those elements without changing their classes.
The definition can be understood as this: there is an operation that acts on elements that belong to a certain object structure. At the same time, this operation is without changing the element classes, under this premise to define a new operation is the essence of the visitor pattern.
The main problem is to solve the problem of stable data structure and variable operation coupling. It is to decouple the data structure and the operation acting on the structure, so that the operation set can evolve relatively freely.
Essence: reserved path, callback realization. Its implementation is mainly through the pre-definition of the calling path, the definition of accept method on the accessed object, and the definition of visit method on the visitor's object; then, when the call really occurs, through the technology of two distributions, using the pre-defined path, callback to the specific implementation of the visitor.
Second, the structure of the visitor pattern
Visitor abstract visitor interface: it defines the behavior of accessing each element (Element), its parameters are accessible elements, and its number of methods is theoretically the same as the number of elements (the number of classes implemented by Element). From this, it is not difficult to see that the visitor pattern requires that the number of element classes cannot be changed (that is, if the number of element classes changes frequently. It is not appropriate to use visitor mode.
ConcreteVisitor specific visitor role: it needs to give the specific behavior generated when accessing each element class.
Element Abstract Node (element) role: it defines a method to accept accept, which means that each element should be accessible to the visitor.
ConcreteElement concrete node (element) role: it provides a concrete implementation of the access method, which is usually provided by the visitor to access the element class.
ObjectStructure structure object role: this is the object structure mentioned in the definition, the object structure is an abstract expression, specifically, it can be understood as a class with container or composite object characteristics, it contains a set of elements (Element), and can iterate these elements for visitors to access.
Third, the usage scenario of the visitor mode
(1) the object structure is relatively stable, but it is often necessary to define new operations on this object structure.
(2) many different and irrelevant operations need to be performed on the objects in an object structure, and these operations need to be avoided from "polluting" the classes of these objects, and you do not want to modify these classes when adding new operations.
Fourth, the advantages and disadvantages of the visitor model
Advantages:
1. The Visitor pattern makes it easy to add new operations visitors make it easy to add operations to components that depend on complex object structures. You only need to add a new visitor to define a new operation on an object structure. Conversely, if each function is scattered over multiple classes, each class must be modified when defining a new operation.
two。 Visitors focus on related operations and separate unrelated actions. Related behaviors are not distributed across the classes that define the object structure, but are concentrated in one visitor. Extraneous behaviors are placed in their respective visitor subclasses. This simplifies both the classes of these elements and the algorithms defined among these visitors. All data structures related to its algorithm can be hidden among visitors.
Disadvantages:
1. It is difficult to add a new ConcreteElement class
The Visitor pattern makes it difficult to add new subclasses of Element. Each time a new ConcreteElement is added, a new abstract operation is added to the Vistor and the corresponding operation is implemented in each ConcretVisitor class. It is sometimes possible to provide a default implementation in Visitor that can be inherited by most ConcreteVisitor, but this is more of an exception than a rule.
So the key question to consider when applying the visitor pattern is which part of the system changes frequently, whether it acts on the algorithm on the object structure, or the classes of the objects that make up the structure. If new ConcretElement classes are added all the time, the Vistor class hierarchy becomes difficult to maintain. In this case, it may be easier to define these operations directly in the classes that make up the structure. If the Element class hierarchy is stable and you keep adding operations to get the modified algorithm, the Visitor pattern can help you manage these changes.
two。 Destroy the package
The visitor approach assumes that the ConcreteElement interface is powerful enough for visitors to do their work. As a result, the pattern often forces you to provide public operations to access the internal state of the element, which may break its encapsulation.
Fifth, the realization of the visitor pattern
Abstract visitor role: an access operation is prepared for each concrete node.
/ / because there are two nodes here, there are two access operations for the corresponding. Public interface Visitor {/ * corresponds to NodeA access operation * / public void visit (NodeA node); / * corresponds to NodeB access operation * / public void visit (NodeB node);}
Specific visitors
/ * * specific visitor VisitorA class * / public class VisitorA implements Visitor {/ * corresponds to the access operation of NodeA * / @ Override public void visit (NodeA node) {System.out.println (node.operationA ()) } / * corresponds to the access operation of NodeB * / @ Override public void visit (NodeB node) {System.out.println (node.operationB ()) }} / * specific visitor VisitorB class * / public class VisitorB implements Visitor {/ * corresponds to the access operation of NodeA * / @ Override public void visit (NodeA node) {System.out.println (node.operationA ()) } / * corresponds to the access operation of NodeB * / @ Override public void visit (NodeB node) {System.out.println (node.operationB ());}}
Abstract node class
/ * Abstract node class * / public abstract class Node {/ * accept operation * / public abstract void accept (Visitor visitor);}
Concrete node class
/ * * specific node class NodeA * / public class NodeA extends Node {/ * accept operation * / @ Override public void accept (Visitor visitor) {visitor.visit (this);} / * * NodeA specific method * / public String operationA () {return "NodeA" }} / * specific node class NodeB * / public class NodeB extends Node {/ * accept method * / @ Override public void accept (Visitor visitor) {visitor.visit (this);} / * NodeB specific method * / public String operationB () {return "NodeB";}}
Structural object role class
/ * * structural object role class * this structural object role holds an aggregation and provides the add () method to the outside world as a management operation on the aggregation. By calling this method, you can dynamically add a new node. * / public class ObjectStructure {private List nodes = new ArrayList (); / * * perform method operation * / public void action (Visitor visitor) {for (Node node: nodes) {node.accept (visitor);}} / * add a new element * / public void add (Node node) {nodes.add (node);}}
Client code
Public static void main (String [] args) {/ / create a structure object ObjectStructure os = new ObjectStructure (); / / add a node os.add (new NodeA ()) to the structure; / / add a node os.add (new NodeB ()) to the structure; / / create a visitor Visitor visitor = new VisitorA (); os.action (visitor) } after reading the above, have you learned how to deeply understand the visitor pattern in the Java design pattern? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.