Getting started with design patterns-Visitor pattern Visitor

Source: Internet
Author: User

// Visitor mode definition: an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the element classes. // From the definition perspective, this mode is similar to the definition of the decoration mode (dynamically add some additional responsibilities to an object ), however, the decoration mode is to enhance or modify functions on the original basis, while the visitor mode is to add new functions for objects. // Pattern structure: // Visitor: Visitor interface, which declares a visit method for all Visitor objects to indicate the functions added to the object structure, theoretically, it can represent any function. // ConcreteVisitor: the visitor implements the object and implements the function to be truly added to the object structure. // Element: abstract Element object, the top-level interface of the object structure, which defines the operation for receiving access // ConcreteElement: The specific element object, the specific object in the object structure, and the accessed object. It usually calls back the real functions of the visitor, at the same time, open your own data for visitors to use // ObjectStructure: object structure, usually contains multiple accessed objects, which can traverse multiple accessed objects, you can also allow visitors to access its elements // instance: Add new features on the basis of the original // The following is the initial code that uses the visitor mode, then you need to expand it based on it // user abstract class public abstract class Customer {private String customerId; private String name; // get set methodspublic abstract void accept (Visitor visitor );} // Enterprise Customer class public class extends isecustomer extends Customer {private String linkman; private String linkTelephone; private String registerAddress; // get set methodspublic void accept (Visitor visitor) {visitor. visitEnterpriseCustomer (this) ;}// Personal Customer class public class PersonalCustomer extends Customer {private String telephone; private int age; // get set methodspublic void accept (Visitor visitor) {visitor. visitPersonalCustomer (this) ;}// Visitor interface public interface Visitor {public void visitEnterpriseCustomer (EnterpriseCustomer ec); public void visitPersonalCustomer (PersonalCustomer pc);} // Visitor, implements the customer's service request function. This function imitates the original function and provides public class ServiceRequestVisitor implements Visitor {public void visitEnterpriseCustomer (EnterpriseCustomer ec) in a specific class) {// get the data System of the enterprise user object. out. prinln (ec. getName () + "enterprise service request");} public void visitPersonalCustomer (PersonalCustomer pc) {// get the data System of the personal user object. out. prinln (pc. getName () + "request service") ;}// ObjectStructurepublic class ObjectStructure {private Collection
 
  
Col = new ArrayList
  
   
(); Public void handleRequest (Visitor visitor) {for (Customer cm: col) {cm. accept (visitor) ;}} public void addElement (Customer ele) {this. col. add (ele) ;}// test the public class Client {public static void main (String [] args) {ObjectStructure OS = new ObjectStructure (); customer cm1 = new EnterpriseCustomer (); cm1.setName ("ABC Group"); OS. addElement (cm1); // Add to OS to add new feature Customer cm2 = new feature isecustomer (); cm2.s EtName ("CDE company"); OS. addElement (cm2); Customer cm3 = new PersonalCustomer (); cm3.setName ("James"); OS. addElement (cm3); ServiceRequestVisitor srVisitor = new ServiceRequestVisitor (); OS. handleRequest (srVisitor) ;}// the following functions are extended to provide a feature for analyzing customers' preferences, you only need to add a specific Visitor to the public class PredilectionAnalyzeVisitor implements Visitor {public void visitEnterpriseCustomer (EnterpriseCustomer ec) {// get the data System of the enterprise user object. out. prinl N ("current to enterprise customers-" + ec. getName () + "Product Preference Analysis");} public void visitPersonalCustomer (PersonalCustomer pc) {// get the data System of the personal user object. out. prinln ("for personal customers now-" + pc. getName () + "Product Preference Analysis") ;}/// same as the request service method on the client // PredilectionAnalyzeVisitor paVisitor = new PredilectionAnalyzeVisitor (); // OS. handleRequest (paVisitor); // conclusion // This mode uses the secondary distribution technology, which is similar to the three-way handshake of the tcp protocol. First, the request gets the control of the other party and the other party accepts it, then start to use the control // Visitor pattern nature: Reserved path, callback implementation // advantage: good scalability, good reusability, separation of irrelevant behavior // disadvantage: It is difficult to change the structure of the image. The encapsulation is broken (the internal data is open to the visitor) // when to use the visitor mode: // 1. if you want to implement a series of operations that depend on the specific classes in the object structure, // 2. if you perform many different and unrelated operations on each element of an object structure, you can use // 3 to avoid class clutter. if the object structure is rarely changed, you need to define new operations for element objects in the object structure. // think: // This mode can be used for all objects, isn't it very convenient to expand this way? // This pattern is strange. It may be a problem with this example. After reading other examples, I feel that this example is not good. Another example is to test how different the two seeds (elements) grow in different environments (Visitor ).
  
 


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.