The strategy design pattern
It is quite a common thing in enterprise applications to have different implementations of specific algorithms and choosing one to use while the application is running. Some examples might include different sorting algorithms that would have different performance for different sizes or types of data, different parsers for various possible representations of data, and so on. The strategy design pattern enables us to:
Note
Define a family of algorithms and select a specific one at runtime.
The strategy design pattern helps with encapsulation as each algorithm can be separately defined and then injected into the classes that use it. The different implementations are also interchangeable.
Class diagram
For the class diagram, let's imagine that we are writing an application that needs to load some data from a file and then use this data somehow. Of course, the data could be represented in different formats (CSV or JSON, in this case), and depending on the file type, we...