I’m designing a framework and I have composite objects that have sub objects that are executed asynchronously or synchronously. For the synchronous executing ones, there’s usually a certain type of logic that has to run in the composite object after a certain sub item finished.
The issue is there’s a number of sub items and for each sub item, there could be a different processing done after each sub item, and because i was aiming for simplicity, i had the synchronous items on a queue and composite item would just pop them one by one and run them.. Now what would be the cleanest way to track the sub items so that I can say something like “after sub item #2 is finished, take the data it returned and do xyz()”?
Consider something like the Visitor Pattern if you have a limited amount of post-processing options.
The composite would implement one method for each type of post-processing that could be necessary, the sub items would implement a method which takes an instance of the composite and calls one of the post-processing methods, depending on which kind of sub item it is. Since you’re already
executeing the sub items, you could make them call the post processing methods at the end of that method instead.An example, skipping the interface definitions: