I have a case where I would like to avoid the switch or if/else structure. I have two objects that perform the same action but are completely different. I have tried to use polymorphism but since they are so different it has not worked.
The idea is to have a method that performs that action and that can grow in the future.
public class Motorbike {
private String handlerbar;
private String wheelA;
private String wheelB;
public void repair(String handlerbar, String wheelA, String wheelB) {...}
...
}
public class Car {
private String steeringWheel;
private String wheelA;
private String wheelB;
private String wheelC;
private String wheelD;
public void repair(String steeringWheel, String wheelA, String wheelB, String wheelC, String wheelD) {...}
}
public class Mechanic {
public void repairVehicle(String type, String handlerbar, String steeringWheel, String wheelA..){
//structure to avoid
if (type == "car") {
Car c = new Car(...);
c.repair(...);
...
} else if (type == "motorbike"){
...
}
}
CodePudding user response:
Here's an implementation using the visitor pattern. Each vehicle has a repeair
method that accepts a mechanic. The vehicle knows what parts it has so it can instruct the mechanic to repair them. That way the mechanic can repair everything without knowing what parts the vehicle has.
You can simply run
Mechanic mechanic = new Mechanic();
mechanic.repairVehicle(new Motorbike());
mechanic.repairVehicle(new Car());
and it will print
Repairing handlebar bar of Motorbike
Repairing wheel front of Motorbike
Repairing wheel rear of Motorbike
Repairing steering wheel steer of Car
Repairing wheel front-left of Car
Repairing wheel front-right of Car
Repairing wheel rear-left of Car
Repairing wheel rear-right of Car
The code for that is
interface Vehicle {
String name();
void repair(Mechanic mechanic);
}
class Motorbike implements Vehicle {
private String handlerbar = "bar";
private String wheelA = "front";
private String wheelB = "read";
@Override
public String name() {
return "Motorbike";
}
@Override
public void repair(Mechanic mechanic) {
mechanic.repairHandlebar(this, this.handlerbar);
mechanic.repairWheel(this, wheelA);
mechanic.repairWheel(this, wheelB);
}
}
class Car implements Vehicle {
private String steeringWheel = "steer";
private String wheelA = "front-left";
private String wheelB = "front-right";
private String wheelC = "rear-left";
private String wheelD = "rear-right";
@Override
public String name() {
return "Car";
}
@Override
public void repair(Mechanic mechanic) {
mechanic.repairSteeringWheel(this, steeringWheel);
mechanic.repairWheel(this, wheelA);
mechanic.repairWheel(this, wheelB);
mechanic.repairWheel(this, wheelC);
mechanic.repairWheel(this, wheelD);
}
}
class Mechanic {
public void repairVehicle(Vehicle vehicle) {
vehicle.repair(this);
}
public void repairHandlebar(Vehicle vehicle, String handlerbar) {
System.out.println("Repairing handlebar " handlerbar " of " vehicle.name());
}
public void repairWheel(Vehicle vehicle, String wheel) {
System.out.println("Repairing wheel " wheel " of " vehicle.name());
}
public void repairSteeringWheel(Vehicle vehicle, String steeringWheel) {
System.out.println("Repairing steering wheel " steeringWheel " of " vehicle.name());
}
}