Dominic Birmingham
Dominic Birmingham

Reputation: 325

What design pattern could be used to connect elements of a composite?

If you have an implementation of a composite design pattern(or any graph/node type system), which design pattern would best describe the process of connecting nodes and creating links between class instances?

My current working example uses the factory pattern to construct each of the class instances but now I'm looking at connecting them in a separate area of code or based on data from a file.

I'm wondering if it is possibly the 'builder' pattern but the examples I've seen are very simplistic and I'm finding it quite a jump to apply the pattern to my domain based on these examples. Perhaps there is another pattern which is more applicable to my domain with a reference to the composite pattern. examples and references would be greatly appreciated.

Many thanks D

EDIT: A little bit more investigation has turned up a nice blog about creational patterns. They say that as a rule of thumb 'builders' often build composites.

http://sourcemaking.com/creational_patterns

Upvotes: 3

Views: 322

Answers (1)

Moshe Shamy
Moshe Shamy

Reputation: 117

If you wish to build the entire composite element at one time, builder is a good choise. for example, if you build graph of roads in a city and you wish to build the entire city - use builder pattern. you can add sub builders to your builder to add neighberhoods so your builder class will not be too big - just do composite for the bulder. but if you wish to add elements after the composite already exist i suggest the following: if you know where to add the element you can provide iterator to your composite and provide add method to the iterator with the index or the new place you wish to add your elements. for example, if you have sorted tree the iterator will have function add that will know where to add the element according to its position in the tree. another option, if you need to add element to the composite that you don't really no in each place, for example, you build city with streets and you wish to add streets next to the shortest street. well, in this case i would probably use predicate or some kind of filter that will be static inner class in your composite type.

Upvotes: 1

Related Questions