Design question : Parallel Inheritance Hierarchies

T

Tomer Ben-David

Hi

In his book "Refactoring improving the design of existing code" martin
fowler writes about the bed smell "Parallel Inheritance Hierarchies".

Lets take this for example :


Person
Student extends Person
Employee extends Person

Now lets say I want to turn those classes into XML, adding the XML to
the Person/Employee/Student classes themselvs could be a possibility
but in real world (at work) those classes tend to grow (and lets
assume for the sake of the example i dont use jaxb etc...), so I would
like to have this functionality out of these classes, in something
that looks like

PersonXMLHelper
StudentXMLHelper extends PersonXMLHelper
EmployeeXMLHelper extends PersonXMLHelper

However Now I have the bed smell "Parallel Inheritance Hierarchies".

Can anyone suggest a better simple design? I come across this problem
over and over again...

Thanks
 
V

VisionSet

Tomer Ben-David said:
Hi

In his book "Refactoring improving the design of existing code" martin
fowler writes about the bed smell "Parallel Inheritance Hierarchies".

Lets take this for example...
I come across this problem
over and over again...

So do I. Why is it a bed(sic) smell?

At the moment I have 3 (M,V,C) parallel hierachies that represent an
MVCesque set of tables.
 
C

Chris Smith

Tomer Ben-David said:
In his book "Refactoring improving the design of existing code" martin
fowler writes about the bed smell "Parallel Inheritance Hierarchies".

Yes, he does.

First and foremost, you should realize that Martin's "bad smells" don't
always represent bad code. He even lists comments as a bad smell!
Fowler says to find smells, and then decide IF the code can be improved
by refactoring. Parallel inheritance hierarchies are, in fact, used to
great effect in several well know design patterns (e.g., see GoF's
Abstract Factory).

I see a few choices in your case:

1. Live with it. This isn't really so bad.

2. Use something that introspects your objects at runtime and saves them
off into an XML format based on the runtime content of the class. This
sidesteps the issues nicely. However, it means that your XML format
needs to be very consistent and predictable -- certainly not appropriate
for matching externally specified formats.

3. Put XML code into the Person classes, which you've already said you
don't want to do.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Alan Gutierrez

Yes, he does.
First and foremost, you should realize that Martin's "bad smells" don't
always represent bad code. He even lists comments as a bad smell!
Fowler says to find smells, and then decide IF the code can be improved
by refactoring. Parallel inheritance hierarchies are, in fact, used to
great effect in several well know design patterns (e.g., see GoF's
Abstract Factory).
I see a few choices in your case:
1. Live with it. This isn't really so bad.

In this case, it knid of is. If for every object, you need to
explicitly write code that assembles a DOM document, or prints XML
to a stream (SAX or Output), you are looking at a lot of tedius
work.
2. Use something that introspects your objects at runtime and saves them
off into an XML format based on the runtime content of the class.
This sidesteps the issues nicely. However, it means that your XML
format needs to be very consistent and predictable -- certainly not
appropriate for matching externally specified formats.

This is probably a good answer in this case. What the OP is really
considering is serialization, which is an "aspect" of the
application, something that happens time and again.

What seems to bother of the OP is the notion that a Person class
contains logic, that to him, not sufficently abstracted. That a
Person class shouldn't know what XML is.
3. Put XML code into the Person classes, which you've already said you
don't want to do.

I think a hybrid would be to create an interface that provides
methods that describe what should be written to XML, and have
an interface that operates on implementations. I'd give an example
here, but there is already a Serializable interface, and you can
store to streams or to XML via something like XStream.

Alan Gutierrez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top