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!

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†