Template pattern with protocol-oriented programming
The template design pattern is a popular behavioral design pattern usually implemented with abstract classes. It helps design algorithms at a high level while letting subclasses or implementers modify or provide parts of it.
Protocol-oriented programming is particularly suited to implementing this pattern, as protocol extensions let you provide default implementations. Default implementations are perfect places to design and implement generic algorithms, as those algorithms can later be applied to a slew of concrete types.
To illustrate this design pattern, we'll build a simple recommendation engine that is tailored to the user's need.
A recommendation engine
A recommendation engine can be thought of as a software system that gives you the most relevant answer based on a list of objects:
protocol RecommendationEngine { associatedtype Model var models: [Model] { get } func filter(elements: [Model]) -> [Model] func sort(elements: [Model...