The strategy pattern
The strategy pattern enables clients to select the best algorithm to use at runtime. Instead of coupling the client with predefined algorithms, the client can be configured with a specific algorithm (strategy) when necessary. In addition, sometimes the choice of algorithm cannot be determined ahead of time because the decision may depend on the input data, the environment, or something else.
In Julia, we can solve the problem using multiple dispatch. Let's consider the case of a Fibonacci sequence generator. As we learned from Chapter 6, Performance Patterns, the calculation of the nth Fibonacci number can be tricky when we implement it recursively, so our first algorithm (strategy) may be memoization. In addition, we ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access