DSA Factory
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.

Quick check

Which Java API call is an example of the strategy pattern?

  1. Alist.sort(comparator): the comparator supplies the ordering algorithm
  2. Blist.size()
  3. Cnew ArrayList<>()
Show the answer

list.sort(comparator): the comparator supplies the ordering algorithm. The algorithm is passed in as an object or lambda.

Lock it in

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