Strategy Pattern in Swift

If we have a task that has multiple different implementations and we might need to use all or some of them during runtime, we can get help from strategy pattern. It is called strategy because according to our strategy, we might use any implementation that fulfills the task.

I will provide an example:

Here, we have a SortAlgorithm protocol and there are two different implementations named MergeSort and QuickSort. SortingManager class initialized with SortAlgorithm and we can change the algorithm on the fly with update function of SortingManager class.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store