Updated: Jan 23
In Strategy pattern, you define multiple solution to same problem and solution can be selected at run time depending on condition.
In Apex, we can use custom setting or custom metadata to define these condition.
Let's jump to solution and understand this pattern via sudo-code.
Suppose you have multiple sorting methods and you need to use appropriate sorting techniques at run time. This condition you can configure in your custom Setting or Custom Metadata. for ex. if you want to choose different technique depending on size of array you can define that in Metadata or Custom Setting.
In this example, we will define 2 sorting technique and use them at run time depending on condition.
In this example, I have used Factory pattern as well, it is to deal with the problem of creating object at runtime.
So, depending on the sorting option you choose, above pattern will call bubble or quicksort classes method automatically.
Ask any question if you have in comment section.