F8: Iterator pattern + Enumerabler + LINQ Flashcards

1
Q

Enumerabler & Iterator pattern

-Förklaring enligt (ELI5)

A

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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Vad är Iterator pattern?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an aggregate object and how does it differ from an iterable object?

A

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):

  • This is THE COLLECTION or group of items. Think of it like a bookshelf full of books. The bookshelf, with all its books, is the aggregate. The aggregate holds the items.

Iterator (or Iterator Object):

  • This is THE TOOL that lets you go through each item in the aggregate, one by one. It’s like having a pointer or a finger that you use to point to each book on the bookshelf in sequence.

The ITERATOR PATTERN involves BOTH:

  • The aggregate provides a way to create an iterator.
  • The iterator provides methods to access items in the aggregate and move through them.

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

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:

  1. Magic Toy Box (with its secret compartments):
    This is the AGGREGATE or collection. It holds all the toys and has its unique way of storing them.
  2. Instructions or Button on the Toy Box:
    This is the ITERABLE OBJECT. When you follow the instructions or press the button, it activates the Toy Wand to help you access the toys.
  3. Toy Wand:
    This is the ITERATOR. When activated by the instructions or button, it knows how to fetch each toy from the box one by one, regardless of where or how they’re stored.
  4. You:
    You’re like the client code. You want to play with the toys without getting into the specifics of where each toy is hidden inside the box. You trust the instructions and the Toy Wand to handle that for you.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are 3 key points of “iterator pattern?”

A
  • The Iterator pattern provides a unified way to access elements of an aggregate object without revealing its underlying representation.
  • By decoupling the iteration logic from the underlying data structure, the iteration algorithm, the iterable object, and the client can change independently.
  • With a standardized iterator interface, the same object can be traversed in multiple different ways and the same traversal algorithm can sometimes be used for multiple different objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the primary goal of iterator pattern?

A

To provide a way to access the elements of an iterable object sequentially without exposing its underlying representation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the difference between:
IEnumerable and IEnumerator:

A
  • IEnumerable is an interface that represents a collection of objects that can be iterated or enumerated over. Any class or struct that implements this interface promises that it can provide an iterator (or “enumerator”) to loop through its items.
  • aka built in interface in C# that represents the aggregate object
  • IEnumerator is the actual iterator interface. It allows the user to traverse through a collection and also keep track of the current position in that collection.
  • Built in interface in C# that represents the iterator object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the benefit of using yield with IEnumerables and IEnumerator?

A

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:

  • First, tell the wand to remember where it left off (which toy was last shown).
  • Then, program it to know how to get the next toy.
  • Also, make sure it knows when there are no more toys left.

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly