Interview mode visitor for getting started with design patterns

Source: Internet
Author: User
Tags call back

Caller Pattern Definition: Represents an operation that acts on individual elements in an object's structure. It enables you to define new actions that act on these elements without changing the individual element classes.

By definition, this pattern is very similar to the definition of a decorative pattern (dynamically adding some additional responsibilities to an object). However, many of the other decorative patterns are based on the original function of the enhancement or modification, and many other visitors mode is to add new features to the object. The interview pattern is suitable for projects that need to add new features and actions to certain classes frequently.

Schema structure://visitor: The Interview interface, declares a visit method for all the visitors object, which is used to represent the function of the object structure, which can theoretically represent the random function//concretevisitor: The detailed visitors implement the object, Implements the function to be really added to the object structure//element: Abstract element object, top interface of object structure, define the operation//concreteelement: Detail Element object. The object structure is a detailed object, and is also the object of the interview, will generally call back the real function of the visitors. At the same time, open up its own data for visitors to use//objectstructure: Object structure. Usually includes several objects that have been interviewed. It is able to traverse multiple visited objects, and also allows visitors to access its elements//instances: Add new functionality on top of IT//the following is an initial code that uses the caller's pattern, and then to expand it on its basis/user abstract class public abstraction classes Customer { private string Customerid;private string Name;//get set methodspublic abstract void Accept (Visitor Visitor);} Enterprise Customer class public class Enterprisecustomer 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 vo ID Accept (Visitor Visitor) {Visitor.visitpersonalcustomer (this);}} User interface public interface Visitor {public void Visitenterprisecustomer (Enterprisecustomer Ec);p ublic void Visitpersonalcustomer (Personalcustomer pc);} A detailed interview with the visitors. To realize the function of customer request for service, this function is to imitate the original function, can give the public class Servicerequestvisitor implements Visitor {public void in the detail class directly Visitenterprisecustomer (Enterprisecustomer EC) {//Received data System.out.prinln for enterprise user objects (Ec.getname () + "Enterprise requests for Service");} public void Visitpersonalcustomer (Personalcustomer pc) {//Received data System.out.prinln (Pc.getname () + "Request for service") for personal user object;}} Objectstructurepublic class Objectstructure {private collection<customer> col = new Arraylist<customer> ( );p ublic void HandleRequest (Visitor Visitor) {for (Customer cm:col) {cm.accept (Visitor);}} public void AddElement (Customer ele) {this.col.add (ele);}} Client test public class Client {public static void main (string[] args) {objectstructure os = new Objectstructure (); Customer CM1 = new Enterprisecustomer () Cm1.setname ("ABC Group") Os.addelement (CM1);//Added to OS. To add a new feature, customer cm2 = new Enterprisecustomer (); Cm2.setname ("CDE Company"); Os.addelement (cm2); Customer cm3 = new Personalcustomer (); Cm3.setname ("Zhang San"); os.addelement (cm3); Servicerequestvisitor srvisitor = new Servicerequestvisitor (); Os.handlerequest (Srvisitor);}} The following is an extension of today's functionality. Add a feature to analyze customer preferences, just add a detailed interview to the public class Predilectionanalyzevisitor implements Visitor {public void Visitenterprisecustomer (Enterprisecustomer EC) {//Received data System.out.prinln for enterprise user objects ("Now to enterprise Customers-" +ec.getname () + " Product preference Analysis ");} public void Visitpersonalcustomer (Personalcustomer pc) {//Received data System.out.prinln for personal user objects ("Now for personal customers-" +pc.getname () + " Product preference Analysis ");}} The use of the client is the same as the request service method//predilectionanalyzevisitor Pavisitor = new Predilectionanalyzevisitor ();//os.handlerequest ( Pavisitor);//summary//This mode uses two distribution techniques. The three-time handshake with the TCP protocol is somewhat similar. First request to get control of each other. The other party to accept. Then start using control//Interview mode nature: Reserved access, callback implementation//Strengths: Good extensibility, good reusability. Segregation-free behavior//Disadvantage: Object structure changes are very difficult, broken encapsulation (by internal data open to visitors)//When to use the caller mode://1. Suppose you want to implement a series of operations//2 that depend on the detailed classes in the object structure for an object structure. Assume that there are very many different and unrelated operations relative to each element in an object structure, in order to avoid these operations clutter the class. Ability to use//3. Assuming that the object structure is very small, it is often necessary to define new operations//thinking for the element objects in the object structure://feel able to use this pattern for all objects, so it is not very convenient to expand it? I always feel this pattern is strange. This is probably the problem of the example itself, look at the other examples, I feel this example is very bad.

Another example is how the two seeds (Element) can grow differently in many different environments (Visitor)



Interview mode visitor for getting started with design patterns

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.