Design pattern Start-visitor mode

Source: Internet
Author: User

Visitor mode

First, the role

(1) The visitor pattern is suitable for the system with relatively stable data structure, separating the processing from the information structures, when the system has a relatively stable data structure and easy to change algorithm, the visitor pattern is more applicable.

(2) The purpose is to encapsulate an operation applied to a data structure element, and to add a new mode of operation without modifying the original system.

Second, class diagram

Third, the realization

(1) IShape defines the current operational requirements and the visitor to be accepted

 Public InterfaceIShape { Public floatGetarea ();  PublicObject Accept (Ivisitor visitor);} Public classTriangleImplementsIShape {floatX1,y1,x2,y2,x3,y3;  PublicTriangle (floatX1,floatY1,floatX2,floatY2,floatX3,floaty3) {         This. X1 =X1;  This. Y1 =Y1;  This. x2 =x2;  This. y2 =Y2;  This. x3 =X3;  This. Y3 =Y3; }     Public floatGetdist (floatU1,floatV1,floatU2,floatv2) {        return(float) math.sqrt ((U1-U2) * (U1-U2) + (V1-V2) * (v1-v2)); } @Override Public floatGetarea () {floatA =getdist (X1,Y1,X2,Y2); floatb =getdist (X1,Y1,X3,Y3); floatc =getdist (X3,Y3,X2,Y2); floats = (a+b+c)/2; return(float) math.sqrt (s* (s-a) * (s-b) * (S-c)); } @Override PublicObject Accept (Ivisitor visitor) {returnVisitor.visitor ( This); }}
View Code

(2) Ivisitor defining the objects to be accepted and the new functions implemented after accepting objects

 public  interface   Ivisitor {Object visitor (Triangle t);}  public  class  Lengthvisitor implements   Ivisitor {@Override 
    public   Object Visitor (Triangle t) {        float  a = T.getdist (t.x1,t.y1,t.x2,t.y2);  float  b = T.getdist (t.x1,t.y1,t.x3,t.y3        );  float  C = T.getdist (t.x3,t.y3,t.x2,t.y2        );     return  a+b+C; }}
View Code

(3) client Customer test class

 Public class Client {    publicstaticvoid  main (string[] args) {        new  lengthvisitor ();         New Triangle (0,0,2,0,0,2);        Shape.accept (visitor);        System.out.println (Shape.getarea ());        System.out.println (Visitor.visitor (Shape));}    }
View Code

Design pattern Start-visitor mode

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: 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.