Complex hierarchy

  • Thread starter Thread starter Sean Chambers
  • Start date Start date
S

Sean Chambers

Hello,

I feel that there is a cleaner way to accomplish what I am trying to
do.

I have 4 different types of classes:

-Request
--Step
---QuestionSet
----Question

That is the hierarchy chain, one request has multiple steps, one step
has multiple questionsets and one questionset has multiple questions

At the moment, I am creating each collection of objects from its
parents ctor. This just feels sloppy and is becoming very hard to read.
I feel like I can use a pattern here. I was thinking an abstract
factory but I guess I just can't see how it would be used here.

Depending on certain variables in Request will determine which Step
objects to build, and which questionSets to add to the steps. A
QuestionSet will always load all of it's questions, but every other
object will have conditional building.

Can anyone point me in the right direction? Like I said, It works as it
is now, but I am trying to refactor to be more elegant because I don't
like my current solution.

Thanks

Sean
 
Take a look at "Head First Design Patterns." This book really helps me
understand patterns. Starting with the "gang of four" book is hopeless.

I think maybe you need nothing more complex than basic factory methods:
* You'll have an interface (in the general sense) for each of the four
types of classes. An abstract class or an interface (per se) I suppose.
* A factory class for each interface. That is the StepFactory, for
example, can instantiate any Step concrete class.
* A concrete class for each subtype that inherits or implements (as
appropriate) it's general interface.

So some omnipotent code will instantiate a RequestFactory, for example,
and pass parameters the RequestFactory.CreateRequest(...) method, let's
say. The question then becomes, does this "omnipotent" code create all
the collections via factories and assemble the parts or Does the
Request have a Step factory and a Step has a QuestionSet factory and so
on.

If the latter, then an instantiated Request will have a StepFactory and
the Request must have enough info in it to know what kind of Step(s) to
tell the factory to make. If not then perhaps the "omnipotent" code
creates it all.

Is this anything like you are thinking?
 
Hi Sean.. Basically you can use public constructors as you are doing or
static
factory methods with a GetInstance method and private constructors. The
factory lets you return null.

More at: Effective Java Programming Language Guide by Joshua Bloch,
Addison-
Wessley, 2001, 252 pp.

Regards,
Jeff
 

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

Back
Top