Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Behavioral Design Patterns: Visitor

DZone's Guide to

Behavioral Design Patterns: Visitor

Need help defining a new operation without changing the classes?

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Our last pattern of the behavioral 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 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 a 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 could be a good fit.

We will start with the visitor interface.

package com.gkatzioura.design.behavioural.visitor;

public interface Visitor {
}


This interface will not specify any methods; however, interfaces that 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.

package com.gkatzioura.design.behavioural.visitor;

public interface Visitable<T extends Visitor> {

     void accept(T visitor);

}


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

package com.gkatzioura.design.behavioural.visitor;

public class LocationRequestExecutor implements Visitable<LocationVisitor> {

    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;
    }

}


package com.gkatzioura.design.behavioural.visitor;

public class RouteRequestExecutor implements Visitable<RouteVisitor> {

    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 then, we shall add the visitor interfaces for these type of executors

package com.gkatzioura.design.behavioural.visitor;

public interface LocationVisitor extends Visitor {

    void visit(LocationRequestExecutor locationRequestExecutor);
}


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.

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.

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 source code on GitHub.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,behavioral design patterns ,design patterns ,visitor ,visitor design pattern ,tutorial ,design pattern tutorial ,sample code

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}