vendredi 21 février 2020

Is the Composite design pattern a good solution for inner nested components?

Trying to design a hierarchy of classes which allow composition (set of elements) I come to a blocker since I don't really know which pattern to apply here, but I know there's one.

The context is the following.

I would like to define a kind of Structure which contain several Element objects. However, within this Element objects I can have more Element objects since they can be nested n degrees inside.

I can think of Decorator since it's like "take an element, and decorate it with whatever more you want", but not sure that this is what I really need since, check the diagram below.

class diagram

What if ConcreteElement contains yet another Element or List<Element>? Is there a sub-pattern for this or can I just infer those within the class as attributes and handle the logic within process()?

Would like to set the elements as structure and then that each element could call himself or any of its child Element objects, same level in the hierarchy, to complete itself.

Aucun commentaire:

Enregistrer un commentaire