Composite of composite? (help needed)

Hi.

I'm writing an app that reads an xml doc and creates an object model from it.

The basic model is this:

At the top level, you have a Project. The Project contains Modules. The Modules can depend on (contain) other Modules, but can also depend on Libraries. In turn, each Library can depend on other libraries.

On top of that, I need to make sure that I only create a single shared instance of each module and library. (for instance, i create library-A which can be used by many modules)

I'm thinking I need to use some sort of composite pattern, but I'm confused cause it seems like I have 2 types of composites but they need to be related.

Just for clarity, here's a simple visual that might explain what i could have at runtime:

(just the objects defined):

Project (myProject)

Module (coolModule) [depends: dumbModule, simpleLib]

Module (okModule) [depends: simpleLib]

Module (dumbModule)

Library (simpleLib) [depends otherLib]

Library (otherLib)

(the tree):

Project (myProject)

Module (coolModule)

Module (dumbModule) (can also be accessed from project)

Library (simpleLib)

Library (otherLib)

Module (okModule)

Library (simpleLib)

Library (otherLib)

I hope this makes sense....

Can anyone tell me if I can use composite for this? If not, is there a good way (patterns or no) to do this?

Thanks!

- Jonathan

[1534 byte] By [jdsysblissa] at [2007-10-2 5:10:03]
# 1

Yes, you could use composite to approach this and you could use it in two different ways.

1) Use it with fixed types for the composite leafs. So you would have Project, Modules, Lib leafs that where composites.This would be the simplest approach. It would be most appropriate where you have a limited number of types of leaf .

2) Where you have lots of types of leaf, or the types of leaf is likely to grow, an alternative would be to make the leaf's type an attibute. This is more complicated because where each leaf has different behaviour (rather than just different attributes) you would need to use the Strategy pattern to attach those different behaviours to the composite leafs.

You should probably search back in the forum, this excercise has been been discussed before.

MartinS.a at 2007-7-16 1:12:57 > top of Java-index,Other Topics,Patterns & OO Design...