There are many ways to generate data and use it in programming. Infinite ways. I just want to talk about two. The two ways that i am in love with. To choose which one to implement is depended on your requirements. I will try to explain when to use them and their key differences in this article.
This is the most common data supply way in programming. In this design, data being supplied by the supplier when the observer make demand for it. To implement this method is very simple. It can be built by just a method and a pair of controlled loops. A function gets called when caller is ready to handle the generated value, then function generates the data and returns it to the caller. Or it can be a little different by using Supplier/Consumer pair, a consumer callback method being passed to a supplier and then supplier calls the consumer with a parameter that contains the generated data after the data is ready. But the thing is, both of these alternatives generate data on the demand. Caller/Consumer determines when to get the data.
Example implementations,
Of course there are many sub types of this design. You need to choose one of them by your requirements. Let me list a few,
Some important things about this design,
#observables #reactive-programming #observer #suppliers #consumer #function