I’m dealing with a Parallel Inheritance Hierarchy and I have an idea for a step by step refactoring and would like opinions on whether it will work or whether there is a better way to do it.
-
Create a Handler class with children that implement the specific behavior required e.g
CarHandlerwith childrenvwHandler,fordHandler,bmwHandler -
Add calls to all the components and restructure it so that the children return the objects neccessary. e.g. Call
vwHandler.getDrivewill return the result ofvwDrive.drive, callvwHandler.getSeatreturnsvwSeat.seat) -
Refactor the other classes so that they provide general functionality rather than specific e.g. Call
vwHandler.getDrivewill returndrive(vwSpec1, vwSpec2)
Example of Handler
public abstract class CarHandler
{
private Car car;
public Car getCar()
{
return car;
}
public Car setCar()
{
car = car;
}
public CarHandler(Car car)
{
this.car = car;
}
public Car getCar()
{
return car;
}
public void setCar(Car car)
{
this.car = car;
}
public void updateCar()
{
Globalizer.updateOnServer(car);
}
public abstract Drive getNewDrive();
public abstract Seat getNewSeat();
}
This approach is working quite well for me actually, I’ve implemented the first two steps and ended up removing several hundred lines of duplication as well as getting a good look at how to further refactor in future. I would highly recommend this approach.