mercredi 25 mars 2015

Handling multiple objects/types for same command with different parameters

Here is my problem statement:



  1. I have some objects that can be either a single object, or have a tree structure of N depth. So maybe I can have objects A,B,C,D. Say A,B,C are single level objects while D have 3 children H,I,J. Among H,I,J, H and I are single objects where J again have children and so on.

  2. All Objects A,B,C,D,H,I,J have a behavior execute().

  3. I have a different class X and all these objects have to use different methods of the SAME INSTANCE OF CLASS X.

  4. How do I handle this scenario in a way that:

    • that one function call executes the execute() method for all the objects on the same level as well as down the hierarchy.

    • I dont have to pass the INSTANCE OF X to all the objects.

    • Each object does some different work in its execute() method.




I could think nothing but the composite pattern but my concern was: - I dont want to pass the INSTANCE OF X to all the objects in the execute() method parameters but the method needs SAME INSTANCE OF X .


Aucun commentaire:

Enregistrer un commentaire