ProgrammingModerateProduct-company placement · Java

Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any comparator. Which design pattern does the Comparator represent?

  1. A. Template Method
  2. B. Observer
  3. C. Strategy
  4. D. Visitor

Correct answer: C. Strategy

Strategy encapsulates an interchangeable algorithm in an object chosen by the client. Template Method would fix the steps in a base class and let SUBCLASSES override them.

More OOP with C++ and Java questions

Exams that ask this topic