views:

79

answers:

3

I am writing an application in Java that produces a XML file using data obtained through JDBC. This is a recursive one-to-many relationship much like the structure of an XML document. It basically looks like this:

Object A contains multiple object B's. Object B contains multiple object C's. And so on.

Is there a handy design pattern I can use for this or should I just throw a collection in each class and live with a complex DAO class?

+2  A: 

XML is basically a tree, most tree structures will translate very well into representing XML.

Neel
+2  A: 

In OO programming land, the way to deal with a variable number of a certain type of object is to put them in a collection. Your class contains a single collection of multiple instances of Type B.

Use collections and deal with the complex DAO class.

...or if you need something more complex, you can use the Composite pattern (You'll still wind up with a collection somewhere though).

Justin Niessner
I originally had high expectations in making these objects immutable container objects. If I have a collection, I want the collection to be immutable as well, defined on object creation.This being the case, all the data will basically have to be known in the controller class that creates object A. This can get really messy and I would like to avoid it if possible.
Melvin
What's the reason for making the object/collections immutable?
Justin Niessner
Just simple code security. This is database information that will not change within the application.
Melvin
Then when you design your class, you can keep an internal List<T> object to store the list, but expose the property as a ReadOnlyCollection. Explained further here: http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Making_Read-Only_Collections_the_Generic_Way
Justin Niessner
A: 

If the parent elements and the children elements have some common attributes you could examine the "Composite Design Pattern"

Read it and see if it suit your needs.

OscarRyz
That is the first design pattern I looked at when creating the application. The problem is that there are no common attributes other than one object containing another.
Melvin