The visitor pattern is one of the 23 design patterns proposed by gof and belongs to the behavior pattern. According to the big talk design model, it is the most complicated and hard to understand model. Definition (derived from gof design pattern): represents 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, we can see that the structure object is a prerequisite for using the visitor mode, and this structure object must have a method to traverse its own objects. This is similar to the Collection concept in Java. Involved roles: 1. ivisitor abstracts the visitor role and declares an access operation interface for the specific element role in the object structure. The name and parameters of this operation interface identify the break element role that sends an access request to a visitor, so that the visitor can directly access it through the specific interface of this element role. 2. concretevisitor. The visitor role implements the interface declared by the visitor. 3. element defines an accept () that uses a visitor as a parameter. 4. The specific elements of concreteelement implement the acceptance operation interface defined by the abstract element. 5. objectstructure object roles, which are essential for using the visitor mode. It has the following features: it can enumerate its elements; it can provide a high-level interface 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 set ). Several features of the visitor mode: the visitor mode decouples the data structure and the operations acting on the structure, allowing the operation set to evolve relatively freely. The visitor mode is suitable for systems with relatively stable data structures and easy-to-change algorithms. The visitor mode makes it easy to increase algorithm operations. If the system data structure objects are easy to change and new data objects are often added, the visitor mode is not suitable. The advantage of the visitor mode is that it is easy to add operations, because adding operations means adding new visitors. The visitor mode aggregates relevant behaviors into a visitor object, and its changes do not affect the system data structure. The disadvantage is that it is very difficult to add new data structures. Applicability: 1) an object structure contains many class objects with different interfaces, and you want to perform operations on these objects dependent on their specific classes. 2) You need to perform many different and unrelated operations on the objects in an object structure, and you want to avoid causing these operations to "pollute" the classes of these objects. The visitor mode allows you to set related operations in a class. 3) when the object structure is shared by many applications, use the visitor mode to allow each application to only include the operations needed. 4) classes that define the object structure are rarely changed, but new operations are often needed in this structure. Changing the object structure class requires redefining the interfaces for all visitors, which may be costly. If the object structure class changes frequently, it may be better to define these operations in these classes. Public class body {public void accept (ivisitor visitor) {visitor. visit (this) ;}} public class engine {public void accept (ivisitor visitor) {visitor. visit (this) ;}} public Class Wheel {private string name; public wheel (string name) {This. name = Name;} string getname () {return this. name;} public void accept (ivisitor visitor) {visitor. visit (this) ;}} public class car {private engine = new engine (); private body = new body (); private wheel [] Wheels = {New Wheel ("front left"), new wheel ("front right"), new wheel ("back left "), new Wheel ("back right")}; Public void accept (ivisitor visitor) {visitor. visit (this); Engine. accept (visitor); body. accept (visitor); For (INT I = 0; I <wheels. length; ++ I) Wheels [I]. accept (visitor );}}
Public interface ivisitor {void visit (wheel); void visit (engine); void visit (body); void visit (car );} public class printvisitor implements ivisitor {@ override public void visit (wheel) {system. out. println ("visiting" + wheel. getname () + "Wheel") ;}@ override public void visit (engine) {system. out. println ("visiting engine") ;}@ override public void visit (body) {system. out. println ("visiting body") ;}@ override public void visit (car) {system. out. println ("visiting car ");}
}
------------------------ Example 2 -----------------------
Visitor mode. without modifying the existing program structure, you can add additional "visitors" to improve the existing code functions. Composition of the visitor mode:
1) Visitor role (visitor): declares an access interface. The Interface Name and method parameters identify the element role that sends a request to a visitor. In this way, the visitor can directly access the element through a specific interface of the element role. 2) specific visitor role (concrete visitor): implements the visitor role (visitor) Interface
3) element: defines an accept operation, which takes a visitor as the parameter. 4) specific element (concrete element): implements the element interface. 5) object structure Role: This is a role required in the visitor mode. It must have the following features: it can enumerate its elements; it can provide a high-level interface to allow the visitor role to access its elements; it can be a combination (combination mode) or a set, such as a list or an unordered set.
Public abstract class customer {private string customerid; private string name;
Public String getcustomerid () {return customerid;} public void setcustomerid (string customerid) {This. customerid = customerid;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}
/*** Access from a visitor */public abstract void accept (visitor );}
/*** Enterprise Customer */public class extends isecustomer extends customer {private string linkman; private string linktelephone; private string registeraddress; Public String getlinkman () {return linkman ;}
Public void setlinkman (string linkman) {This. linkman = linkman ;}
Public String getlinktelephone () {return linktelephone ;}
Public void setlinktelephone (string linktelephone) {This. linktelephone = linktelephone ;}
Public String getregisteraddress () {return registeraddress ;}
Public void setregisteraddress (string registeraddress) {This. registeraddress = registeraddress ;}
@ Override public void accept (visitor) {// method of calling back the visitor object visitor. visitenterprisecustomer (this );}}
/*** Individual customer */public class personalcustomer extends customer {private string telephone; private int age; Public String gettelephone () {return telephone ;}
Public void settelephone (string telephone) {This. Telephone = telephone ;}
Public int getage () {return age;} public void setage (INT age) {This. Age = age ;}
@ Override public void accept (visitor) {// method of calling back the visitor object visitor. visitpersonalcustomer (this );}}
/* ** Visitor interface */public interface visitor {// access an enterprise customer, which is equivalent to adding the public void visitenterprisecustomer (enterprisecustomer EC) to the enterprise customer. // access an individual customer, public void visitpersonalcustomer (personalcustomer PC );}
/*** Specific visitor for Customer Preference Analysis */public class predilectionanalyzevisitor implements visitor {@ override public void visitenterprisecustomer (enterprisecustomer EC) {// todo analyzes system based on previous purchase history, potential purchase intention, development trend of the customer's industry, and development trend of the customer. out. println ("current to enterprise customers" + EC. getname () + "Product Preference Analysis ");}
@ Override public void visitpersonalcustomer (personalcustomer PC) {system. out. println ("for personal customers now" + PC. getname () + "Product Preference Analysis") ;}/ *** specific visitor, implement the customer's service request function */public class servicerequestvisitor implements visitor {@ overridepublic void visitenterprisecustomer (enterprisecustomer EC) {// todo enterprise customer's specific service request system. out. println (EC. getname () + "enterprise service request ");}
@ Overridepublic void visitpersonalcustomer (personalcustomer PC) {// the specific service request system submitted by the todo customer. out. println ("customer" + PC. getname () + "request service ");}} public class objectstructure {/*** collection of customers to operate */private collection <customer> Col = new arraylist <customer> (); /*** provides high-level interfaces for client operations. The specific functions are determined by the visitor passed in by the client * @ Param visitor the visitor required by the client */Public void handlerequest (visitor) {for (customer CM: COL) {cm. accept (visitor) ;}}/*** sets up the object structure, to add elements to an object * different object structures have different construction methods * @ Param ele add to the object's structure element */Public void addelement (customer Ele) {This. col. add (Ele );}}
Public class client {public static void main (string [] ARGs) {objectstructure OS = new objectstructure (); customer CRF = new enterprisecustomer (); CRF. setname ("Iron Xia"); OS. addelement (CRF );
Customer cm2 = new enterprisecustomer (); cm2.setname ("CDE company"); OS. addelement (cm2 );
Customer cm3 = new personalcustomer (); cm3.setname (""); OS. addelement (cm3 );
Servicerequestvisitor srvisitor = new servicerequestvisitor (); OS. handlerequest (srvisitor );
Predilectionanalyzevisitor pavisitor = new predilectionanalyzevisitor (); OS. handlerequest (pavisitor );
Worthanalyzevisitor wavisitor = new worthanalyzevisitor (); OS. handlerequest (wavisitor );}}