Software Development

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.

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.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dmytro
Dmytro
2 years ago

It is not clear when the method executeRequest is called.

Back to top button