Implementations Flashcards

1
Q

How do we implement a strategy pattern?

A

Create a context class that has
- A private variable to store strategy implementation
- a constructor which takes in a strategy
- a method setStrategy which takes in a strategy
- a method perform which takes in an object to perform strategy on

Create a strategy interface
- One method inside which performs an action

Create a new class which implements the strategy and override the one method to change how action is performed

Create a main inside a client class where you create a context object with an implemented strategy as a parameter. Create an object then perform the strategy on the object by passing into the method

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

How do we implement an iterator pattern?

A

Outerclass with list of what you want to iterate through which implements iterable
- overrides one method iterator() of type Iterator<T> which returns an Iterator<T> by calling new innerclass()</T></T>

Have an innerclass with variable count which implements Iterator<T>
- overrides two methods, hasNext() (checks if count < array.size() and Next(), returns the element at that pos in list, increment count)</T>

In main create an outerclass object, then create an innerclass object through the outerclass. Do a while hasNext, System.out.println ( next())

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