mardi 24 février 2015

Convert a Composite Pattern to a new one with additional behavior

I'm running a little problem with our design. We have an internal framework in whcih we are using the composite pattern to represent a hierarchical data structure (sorry I cannot post images due to reputation...):



  • IEntity: with a "Name" property"

  • LeafEntity (abstract): "Name" is virtual

    • LeafEntityA

    • LeafEntityB

    • ....



  • ContainerEntity (abstract): "Name" is virtual, "Children" is IList

    • ContainerEntityA

    • ContainerEntityB

    • ....




This hierarchical data structure is returned by a "Read file" method from the framework to our application and is also used by other application.


Now in our application, we need exactly the same data structure (a composite pattern) but with additional behavior: we need an extra GetCompareText() method for each entity. And this method should be definable in each entity:



  • ContainerEntity should implement the default behavior for the GetCompareText method for "Containers"

  • LeafEntity should implements the default behavior for the GetCompareText method for "Leaves"

  • Some leaf entities can ovveride the behavior (For example, LeafEntityA must override the behavior, but LeafEntityB must have the default behavior)

  • Some Container entities can override the default behavior (ContainerEntityA uses default behavior, when ContainerEntityB overrides it).


How can we achieve this ? Do we need another Composite Pattern in our Application? Is Decorator pattern an option ?


I hope I was clear enougth


Thanks for your assistance!


Benoît


Aucun commentaire:

Enregistrer un commentaire