In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the advantages of the Visitor pattern of the Design pattern Series". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the advantages of the Visitor pattern in the design pattern series.
Basic introduction
The basic concept of the Visitor pattern: encapsulating operations that act on elements in a data structure. An important feature is that some new operations can be defined without changing the data structure.
To put it simply, the main function of the visitor pattern is to separate the "data structure" from the "data operation" to solve the problem of coupling between the two.
The basic implementation principle of the visitor pattern is to add an interface to the accessed class to receive visitors.
In general, when we need to perform many different operations on elements in a data structure, and these operations are not related to each other, and we want to avoid "contaminating" these elements because of these operations, consider using the visitor pattern.
Visitor Mode UML Class Diagram
Class diagram explanation
Visitor: abstract access role; usually several elements in the data structure correspond to providing an access operation (method) for each element in the class. ConcreteVisitor: specific visitor role; inherits Visitor and implements all the methods defined in it. Element: abstract element role; this class defines an accept (receive) method for receiving visitors. ConcreteElement: concrete element role; inherits Element and implements the accept method defined in it. ObjectStruture: this class defines the data structure (object structure), manages all elements, and enumerates its elements (that is, traversal).
Case explanation
Case study: develop an employee approval function, specifically, employees in different roles can approve "agree" and "disagree".
Abstract employee class = > corresponding Element (abstract element role)
Public abstract class Workers {/ / provides a method public abstract void accept (Action action) for visitors to access;}
Specific employee category
/ * Manager * / public class Manager extends Workers {/ * dual dispatch is used here. * first dispatch: the specific Action is passed to the Manager as a parameter in the client. * the second dispatch: the Manager class calls the specific method in Action and passes itself as a parameter. * / @ Override public void accept (Action action) {action.managerVerify (this);}} / * team leader * / public class GroupLeader extends Workers {@ Override public void accept (Action action) {action.groupLeaderVerify (this);}}
Abstract behavior class = > corresponding Visitor (abstract access role)
Public abstract class Action {/ / Manager for approval public abstract void managerVerify (Manager manager); / / team leader for approval public abstract void groupLeaderVerify (GroupLeader groupLeader);}
Specific behavior class
/ * agree * / public class Agree extends Action {@ Override public void managerVerify (Manager manager) {System.out.println ("the manager's audit result is yes!") ; @ Override public void groupLeaderVerify (GroupLeader groupLeader) {System.out.println ("the team leader's audit result is agreed!") ;}} / * disagree * / public class Disagree extends Action {@ Override public void managerVerify (Manager manager) {System.out.println ("the manager's audit result is disagree!") ; @ Override public void groupLeaderVerify (GroupLeader groupLeader) {System.out.println ("the team leader's audit result is disagreed!") ;}}
ObjectStructure class
Public class ObjectStructure {/ / maintains a collection private List peoples = new ArrayList (); / / adds public void attach (Workers workers) {peoples.add (workers);} / removes public void detach (Workers workers) {peoples.remove (workers);} / / displays evaluation public void display (Action action) {for (Workers people: peoples) {people.accept (action) }}}
Client test class
Public class Client {public static void main (String [] args) {ObjectStructure objectStructure = new ObjectStructure (); / / add objectStructure.attach (new Manager ()); objectStructure.attach (new GroupLeader ()); / / agree to Agree agree = new Agree (); objectStructure.display (agree);}}
Execution result
Summary
Advantages:
1. The visitor model conforms to the principle of single responsibility. 2. Data structure and data operation can be decoupled. 3. The problem of pollution caused by operating elements is avoided. 4. it also greatly increases the flexibility in the case of expansibility of the program.
Disadvantages:
1. Because the specific element publishes the implementation details to the visitor, the visitor pattern violates the Dimitt rule. Doing so can make it difficult for elements to change. 2. Because visitors rely on concrete elements rather than their abstract parents, all of this pattern also violates the dependency inversion principle.
At this point, I believe you have a deeper understanding of "what are the advantages of the Visitor pattern in the design pattern series". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.