The question in broad terms:
I have an object with a method that performs a long iterative process and I have a second object which is designed to log/monitor the happenings within that long iterative process. What is the best design/design-pattern to observe and monitor the progress of the iterative process?
The specific problem I’m working on:
I have a RandomWalker object with a method InitiateRandomWalk() which causes the random walker to walk thousands of steps. I also have a HeatMaps object which describes a set of heat map images which are rendered by analyzing every step of the randomly walked path.
I don’t want to wait until the completion of the InitiateRandomWalk() method to pass the path data to the Heatmaps object and start rendering the heat map. Instead, I want my Heatmaps object to observe and log the random walk data as it happens.
Some possibilities:
- I could make the heatmap logging method
publicandstaticand call that method from within theInitiateRandomWalk()method but that would be bad design. - I could have InitiateRandomWalk() return an
IEnumerableandyield returneach step and then pass each step toHeatmaps. - I could pass the
Heatmapsobject to theInitiateRandomWalk()method as a parameter.
Which design/design pattern would be best?
From the usage of yield return keyword construct, I’m assuming you’re using C#. Using that construct with Reactive Framework is a PERFECT approach (although somewhat advanced) to solving your problem. Here’s a good overview of how to use it in action.
The other option is to simply declare an event StepTaken on the RandomWalker. The HeatMap would subscribe to the event and InitiateRandomWalk would fire the event every time it generates a step.