Free Design patterns lessonsDesign patterns · Stage 2 · Behaviour and communication · Step 1
Strategy
Swapping an algorithm by putting each variant behind an interface and handing one to the object that needs it. About 12 minutes.
Which Java API call is an example of the strategy pattern?
- Alist.sort(comparator): the comparator supplies the ordering algorithm
- Blist.size()
- Cnew ArrayList<>()
Show the answer
list.sort(comparator): the comparator supplies the ordering algorithm. The algorithm is passed in as an object or lambda.
4 questions on this idea
Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.
Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve