I have a custom made collection that has many modes of objects generations inside of it.
It can generate everything, one object at a time or N objects at a time.
I would like to have the option to switch between implementations of generation on runtime, and even maybe create new ones.
I am looking for something with this kind of syntax:
foreach(var obj in myCollection.EnumerateAs(new LazyEnumerator())
{
// ...
}
My problems are:
I don’t know what does EnumerateAs() return? I am assuming that it’s IEnumerator but will it still be the enumerator of my list?
Does LazyEnumerator inherit from IEnumerator?
How is it aware of myCollection?
The return value of your
EnumerateAs()should beIEnumerable<T>, where T is the type of object contained in your collection. I recommend reading more about yield return, as this may help you to understand how enumeration works. There is no default class for providing an enumeration ‘strategy’, but you can easily implement something like this by using yield return on the underlying collection in various ways.It’s not clear from your question exactly how the enumeration strategies would interact with your collection class. It looks like you might be after something like:
Note that you might also replace the strategy class with a simple strategy
Func<MyCollection, IEnumerable<T>>, but the above matches your desired syntax most closely.