-1

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.

  1. Create a Handler class with children that implement the specific behavior required e.g CarHandler with children vwHandler, fordHandler, bmwHandler

  2. Add calls to all the components and restructure it so that the children return the objects neccessary. e.g. Call vwHandler.getDrive will return the result of vwDrive.drive, call vwHandler.getSeat returns vwSeat.seat)

  3. Refactor the other classes so that they provide general functionality rather than specific e.g. Call vwHandler.getDrive will return drive(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();    
}
FooBar
  • 1,663
  • 2
  • 12
  • 19
  • Could you provide us with an example ? – Grooveek Jan 20 '12 at 08:48
  • @Grooveek Added an example of the handler – FooBar Jan 20 '12 at 09:06
  • Even before that, do the different type of handlers have different behaviour? Are you sure you cannot reduce it to a flag (a member with an enum with values of the brands)? – Viruzzo Jan 20 '12 at 09:08
  • @Viruzzo The point is giving the handlers the potential for different behavior so that they can absorb the different behavior of the classes that they are currently referencing so that those classes and the parallel hierarchy problem that they represent can be eliminated by consolidating the behavior that needs to be different into one place. – FooBar Jan 20 '12 at 10:08
  • My question was about the model itself, I was wondering what kind of different behaviour could handlers have based just on brand; it wasn't a matter of where it was implemented. Basically it was a question of if it was possible to reduce it to a single hierarchy problem. – Viruzzo Jan 20 '12 at 10:18
  • @Viruzzo Unfortunately, no, this is a simplified model that will be applied to a more complex system where this is not an option – FooBar Jan 20 '12 at 10:54

2 Answers2

0

I don't really understand your problem, as I don't see the relation of your example to a parallel inheritance hierarchy, but I don't like the protected field car in your approch.

For a few more details see my answer here. The accepted answer there is a good example of how to encapsulate behaviour in the base class.

Community
  • 1
  • 1
DanT
  • 3,960
  • 5
  • 28
  • 33
  • yeah, I know that's bad form, I'll convert that to a normal accessor and mutator, thanks for the heads up. – FooBar Jan 20 '12 at 10:48
  • This should be a comment not an answer. If you have multiple objects such as vwDrive, vwSeat, vwEtc this is an indication of parallel hierarchies that implement object specific behavior in multiple locations that should ideally be centralized into one specific class and other general toolsets that the central specific class can call with differing parameters. – FooBar Jan 20 '12 at 10:53
  • Do vwSeat and bmwSeat need to have different behaviour, i.e. different type? Or could they just have different field values? Btw. I have not enough reputation to leave comments. – DanT Jan 20 '12 at 12:24
0

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.

FooBar
  • 1,663
  • 2
  • 12
  • 19