Enumerabler & Iterator pattern
-Förklaring enligt (ELI5)
The “iterator pattern” is like having a key to open each carriage’s door so you can see what’s inside. It helps you go from one carriage to the next, opening one door at a time.
Now, not all trains are made the same. Some trains allow you to go through each carriage with a key, while others might not. If a train has a special sign that says “You can go through each carriage with a key”, that train is “enumerable.”
Vad är Iterator pattern?
Iterator pattern is one of the foundational design patterns that offers a sequential way to access elements of an iterable object without needing to expose the object’s underlying data structure.
What is an aggregate object and how does it differ from an iterable object?
An iterable object is also often called an ‘aggregate object’ because it ‘aggregates’ values. Anything that can produce a sequence of values can be thought of as an iterable or aggregator.
However this is wrong since the iterator object and the aggregate object are related but technically different concepts in the iterator pattern.
Aggregate (or Aggregate Object):
Iterator (or Iterator Object):
The ITERATOR PATTERN involves BOTH:
However, in conversations or some contexts, people might occasionally use the term “aggregate” more broadly to refer to both the collection and the way you access it. But technically, the iterator and the aggregate are distinct parts of the iterator pattern.
Förklara följande stycke:
The Iterator pattern decouples the client code from the iterable object’s internal structure, allowing items to be navigated and accessed without providing direct access to the underlying representation.
Imagine you have a magic toy box. Inside this box, toys are arranged in ways you don’t know about. Some toys might be in tiny drawers, some in secret compartments, and some in little bags.
Now, you want to play with each toy one by one, but you don’t want to learn about all the secret compartments and special ways the toys are stored. You just want to play with them.
Along comes a special magic wand, the “Toy Wand”. Every time you wave it, the wand gives you the next toy from the box, no matter where it’s hidden. You don’t need to know where each toy was or how they were stored. The wand takes care of that.
Here is how the example about a Magic toy box maps to the concept:
What are 3 key points of “iterator pattern?”
What is the primary goal of iterator pattern?
To provide a way to access the elements of an iterable object sequentially without exposing its underlying representation.
Explain the difference between:
IEnumerable and IEnumerator:
What is the benefit of using yield with IEnumerables and IEnumerator?
The fundamental idea behind IEnumerator (the wand in previous examples) is to produce items one by one. The magic of yield is more about how easy and convenient it makes to write such a method in C#.
Without yield:
* Imagine if your wand didn’t understand the magic word “yield.” To show each toy one by one, you’d have to manually program your wand for every single toy in the magic box:
This is a lot of work for every box of toys!
With yield:
* With the magic word “yield.” Suddenly, the wand becomes smarter. You don’t need to tell it how to remember its place or figure out the next toy. You just say “yield” and point to the toy, and the wand handles the rest. The wand, thanks to “yield,” knows where it left off and automatically fetches the next toy the next time you use it.