dimanche 1 janvier 2017

Naming Convention for Adapter Pattern? Why is it TargetAdapter, not AdapteeAdapter?

In client code I program to Vehicle interface, so in my Client I have Vehicle interface, class Car implements Vehicle, class Airplane implements Vehicle.

So Client code is

public class ClientExample1 {

public void appLogic() {

    List<Vehicle> lst = new ArrayList<>();
    lst.add(new Car());
    lst.add(new Airplane());

    //now I want add Spaceship to the list.
    //Spaceship methods do the same logic, but they are named differently
    //so write adapter class and use it
    lst.add(new VehicleAdapter());

    for (Vehicle vehicle : lst) {
        vehicle.go();
        vehicle.stop();
    }

}

public static void main(String[] args) {
    new ClientExample1().appLogic();
}

}

I implement Adapter, for example thru inheritance (Class Adapter type):

    // we want use Spaceship in Client (ClientExample1)
//
// Adapter type: Class Adapter (inheritance of Spaceship)
// Naming convention for Adapter - TargetAdapter
//
// Target - Vehicle interface
// Target methods - methods of Vehicle interface
// Adaptee - Spaceship class
//spelling: adapter = adaptor. Both correct. Adapter - more often, both in US & GB

class VehicleAdapter extends Spaceship implements Vehicle {

    @Override
    public void go() {
        start(30, 30); //Spaceship method, same as go() but different name
    }

    @Override
    public void stop() {
        land(30, 30);
    }

}

My question is: why in all examples all over internet adapter is named after target class/interface (Vehicle in this example) - VehicleAdapter?

I think it is more sense to name adapter like this: class SpaceshipAdapter.

If I need to adapt another class UBoat to be used instead of Vehicle interface with (another) adapter, how shall I name such adapter? VehicleAdapter2 ? UBoatAdapter could have been a reasonable name.

So what are the best practise and why such (informal) naming convention exists, and shall programmers follow this convention?

Aucun commentaire:

Enregistrer un commentaire