Home » Software Development » Behavioural Design Patterns: Visitor

About Emmanouil Gkatziouras

Emmanouil Gkatziouras
He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.

Behavioural Design Patterns: Visitor

Our last pattern of the behavioural design patterns is going to be the visitor pattern.

We use the visitor pattern when we want to make it possible to define a new operation for classes of an object structure without changing the classes.

Imagine the scenario of a software that executes http requests to an api. Most http apis out there have certain limits and allow a specific number of requests to be executed per minute. We might have different class that executes requests and also takes into consideration the business logic with regards to the apis that they interact.
In case we want to inspect those calls and print some information or persist request related information to the database the visitor pattern might be a good fit.

We will start with the visitor interface.

1
2
3
4
package com.gkatzioura.design.behavioural.visitor;
 
public interface Visitor {
}

This interface will not specify any methods, however interfaces which extend it will contain methods visit with specific types to visit. We do this in order to be able to have loosely coupled visitor implementations (or even composition based visitors).

Then we shall implement the visitable interface.

1
2
3
4
5
6
7
package com.gkatzioura.design.behavioural.visitor;
 
public interface Visitable {
 
     void accept(T visitor);
 
}

Based on the above we shall create our request execution classes which are visitable.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.gkatzioura.design.behavioural.visitor;
 
public class LocationRequestExecutor implements Visitable {
 
    private int successfulRequests = 0;
    private double requestsPerMinute = 0.0;
 
    public void executeRequest() {
        /**
         * Execute the request and change the successfulRequests and requestsPerMinute value
         */
    }
 
    @Override
    public void accept(LocationVisitor visitor) {
        visitor.visit(this);
    }
 
    public int getSuccessfulRequests() {
        return successfulRequests;
    }
 
    public double getRequestsPerMinute() {
        return requestsPerMinute;
    }
 
}
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.gkatzioura.design.behavioural.visitor;
 
public class RouteRequestExecutor implements Visitable {
 
    private int successfulRequests = 0;
    private double requestsPerMinute = 0.0;
 
    public void executeRequest() {
        /**
         * Execute the request and change the successfulRequests and requestsPerMinute value
         */
    }
 
    @Override
    public void accept(RouteVisitor visitor) {
        visitor.visit(this);
    }
 
    public int getSuccessfulRequests() {
        return successfulRequests;
    }
 
    public double getRequestsPerMinute() {
        return requestsPerMinute;
    }
}

And the we shall add the visitor interfaces for these type of executors

1
2
3
4
5
6
package com.gkatzioura.design.behavioural.visitor;
 
public interface LocationVisitor extends Visitor {
 
    void visit(LocationRequestExecutor locationRequestExecutor);
}
1
2
3
4
5
6
package com.gkatzioura.design.behavioural.visitor;
 
public interface RouteVisitor extends Visitor {
 
    void visit(RouteRequestExecutor routeRequestExecutor);
}

The last step would be to create a visitor that implements the above interfaces.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
package com.gkatzioura.design.behavioural.visitor;
 
public class RequestVisitor implements LocationVisitor, RouteVisitor {
 
    @Override
    public void visit(LocationRequestExecutor locationRequestExecutor) {
 
    }
 
    @Override
    public void visit(RouteRequestExecutor routeRequestExecutor) {
 
    }
}

So let’s put em all together.

01
02
03
04
05
06
07
08
09
10
11
12
13
package com.gkatzioura.design.behavioural.visitor;
 
public class VisitorMain {
 
    public static void main(String[] args) {
        final LocationRequestExecutor locationRequestExecutor = new LocationRequestExecutor();
        final RouteRequestExecutor routeRequestExecutor = new RouteRequestExecutor();
        final RequestVisitor requestVisitor = new RequestVisitor();
 
        locationRequestExecutor.accept(requestVisitor);
        routeRequestExecutor.accept(requestVisitor);
    }
}

That’s it! You can find the sourcecode on github.

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Behavioural Design Patterns: Visitor

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of