Follow treslines by email clicking Here!

Tuesday, August 12, 2014

Programming Design Pattern - Visitor Pattern Applied - Best Practise

Hi there!

Today i'm gonna show the visitor design pattern in action. The visitor design pattern is a very useful programming design pattern while dealing with responsibilities that are not part of an exsiting implementation.

I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.

First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.

The UML Diagram of the Visitor Pattern


Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.


The visitor and visitable

In the example bellow, we will use the visitor to extract information from an existing car structure. Let's say you have a program with deals with a lot of different vehicles registering it and storing information about it. We will use it, to extract some information, that is not part of the responsibility from this structure. Here we define the necessary interfaces and classes to implement it.

public interface Visitor {
    < T extends Visitable > void visit(T visitable, VisitableType type);
}
public interface Visitable {
    < T extends Visitor > void accept(T visitor);
}
public enum VisitableType {
    CAR, BUS, TRUCK;
}

The concrete visitable

Those classes are the instances who accepts visitors. Let's see how they are implemented...

public class Bus implements Visitable {
    @Override
    public < T extends Visitor > void accept(T visitor) {
        visitor.visit(this, VisitableType.BUS);
    }
    public void fuelType(){
        System.out.println("Bus> Fuel: Gas");
    }
}
public class Car implements Visitable {
    @Override
    public < T extends Visitor > void accept(T visitor) {
        visitor.visit(this, VisitableType.CAR);
    }
    public void fuelType(){
        System.out.println("Car > Fuel: Gasoline and Alcohol");
    }
}
public class Truck implements Visitable {
    @Override
    public < T extends Visitor > void accept(T visitor) {
        visitor.visit(this, VisitableType.TRUCK);
    }
    public void fuelType(){
        System.out.println("Truck> Fuel: Diesel");
    }
}

The concrete visitors

When the visitables accepts some visitor, they imediatelly calls the method visit from it passing itself to it. This way, the visitors can use some attributes of it to extract infos or to process what ever you need to process. Let's take a look inside of it to understand it.

// VISITOR
public class Fuel implements Visitor {
    @Override
    public < T extends Visitable > void visit(T visitable, VisitableType type) {
        switch (type) {
        case CAR:
            ((Car)visitable).fuelType();
            break;
        case BUS:
            ((Bus)visitable).fuelType();
            break;
        case TRUCK:
            ((Truck)visitable).fuelType();
            break;
        default:
            break;
}}}

Testing it

The test bellow shows the visitor in action. We are extracting infos from the cars and showing to the user.

public class Client {
    public static void main(String[] args) {
        // VISITOR
        final Visitor info = new Fuel();
        // VISITABLES
        final Visitable car = new Car();
        final Visitable truck = new Truck();
        final Visitable bus = new Bus();
        // VISITING VISITABLES
        car.accept(info);
        truck.accept(info);
        bus.accept(info);
    }
}

That's all! I hope you like it!

No comments:

Post a Comment