In this readme, my best attempt of answering the given questions for task A of Lab 1 is presented:
In this readme, answers are given to questions of task A of Lab 1:
#### 1. First, you should describe which elements implement the Strategy design pattern, and relate the concepts in that design pattern to the example code given. You should be able to reason with respect to both the basic example on GitLab and the elaborate one given above.
#### 1. First, you should describe which elements implement the Strategy design pattern, and relate the concepts in that design pattern to the example code given. You should be able to reason with respect to both the basic example on GitLab and the elaborate one given above.
The elements implementing the Strategy design pattern in the GitLab code is these:
| Context | Interface | Concrete |
|------------|------------|-----------|
| Enumerable | IPredicate | Ananomous |
| Enumerable | IAction | Anonymous |
The ananomous implementations of IPredicate probably resembles the implementations of `ModulePredicate(0)` and `ModulePredicate(1)` in the example code.
The `IAction` implementation handles printing all elements of an instance of `Enumerable`, `evenNumbers` and `oddNumbers`, with the prefix "Even number " or "Odd number ", respectively.
In the example code of the assignment these elements applies to the Strategy design pattern:
#### 2. Second, you should explain how the current implementation violates the principles of the Iterator design pattern (applicable to both the basic example on GitLab and the elaborate one given above)
#### 2. Second, you should explain how the current implementation violates the principles of the Iterator design pattern (applicable to both the basic example on GitLab and the elaborate one given above)
`Enumerable` doesn't implement an AbstractEnumerable interface. Also, the examples doesn't include a concrete implementation of an Iterator interface. Instead, in the GitLab-code, the `Enumerable` internally uses an iterator as a behaviour derived from `Iterable<T>`.
#### 3. Third, you should correct the implementation provided so that it works as intended. Explain why it did not work initially.
#### 3. Third, you should correct the implementation provided so that it works as intended. Explain why it did not work initially.
#### 4. Fourth, you should explain how you encapsulated all objects in interfaces to enforce Interface Segregation, which states that all clients should use clientspecific interfaces. This assumes that all methods accessible through an interface is useable by any client. In your explanation, provide sufficient code to explain how to extend the current implementation to the elaborate functionality given in the example above, by providing interfaces between the different classes.
The biggest issue was that the `where`-method returned the instance of the `Enumerable` of which it was called from. In this example, this behaviour is not very nice since both times that method is used (to filter out a certain type of numbers) it expects all numbers to be present. Before fixing that, the predicate used to get `oddNumbers` is applied to an `enumerable` that only contains even numbers. To fix this, I decided to return a new `Enumerable`-instance instead of the previous one.
\ No newline at end of file
There was also an issue where the `IAction` previded in `forEach` always was applied to the last element of the `Enumerable`. I fixed that by checking `predicate.accept(item)` after the loop finishes. This check can make `next()` return null, so I also added a null-check in `forEach`.
#### 4. Fourth, you should explain how you encapsulated all objects in interfaces to enforce Interface Segregation, which states that all clients should use clientspecific interfaces. This assumes that all methods accessible through an interface is useable by any client. In your explanation, provide sufficient code to explain how to extend the current implementation to the elaborate functionality given in the example above, by providing interfaces between the different classes.