In Strategy Design Pattern, what we do is
- Create a common interface.
- Implement a set of classes using that interface with overridden method(s).
- Let the run time to choose the actual class for an object which has the same type with that common interface and call the overridden method(s) which will resolve correctly according to the class.
My question is, Isn't it the basic example of polymorphism and method overriding we learn?
other than the possibility of using an abstract class too, replacing the common interface.
Polymorphism: there is an interface or base class where concrete implementations can be exchanged.
Strategy Pattern: A part of a logic is put into a separate class accessed by an interface in order to exchange it. It's therefore using polymorphism in a specific way.
Many OO patterns are using polymorphism. And most of them are kind of basic. Let's take the composite pattern, which I always refer to as the one which shows the power of polymorphism best.
Strategy can be used as an example of Polymorphism in some manners. But it's not a good start for OOP newbies as it's more complex than other patterns.
Yes you can replace interface with abstract class and even normal class. It doesn't matter. "Abstraction" in OOP is not all classes must be derivative of an abstract class or interface.
Strategy_pattern uses run-time polymorphism to make algorithms interchangeable within that family.
Replacing one algorithm with-in family of multiple algorithms is one part of Strategy pattern. Strategy pattern can use Context also.
You can achieve replacement of algorithm at run time without using interface/run-time polymorphism.
Call a method in Context by passing a condition. Have switch case statements calling different methods based on individual case. This approach is not as clean as run-time polymorphism but still you can achieve it.
Strategy UML:
Related posts:
Confused about strategy design pattern
Strategy implementation can be even simpler in dynamic languages such Javascript. Whenever you pass a callback arround, your are probably extending a Strategy implementation.
Example: sorting arrays
Javascript's native arrays have a method called
sort
that will return a brand new sorted array. It can have one parameter, thecomparisonFunction
, a callback that will compare 2 items in the array and return:1
if the first element is to be considered higher than the second.0
if it is to be considered equal-1
if it is to be considered lower thenSo, you can use whatever strategy you want to sort the array:
When you are creating your on libraries and expect a callback that will do some work on your data, you are probably implementing the Strategy pattern.
I'd like to add that when using polymorphism you must typically destroy the object and replace it with a new instance. It could be expensive to constantly create/destroy the new instance. The strategy pattern is one way you can implement lazy loading/storing previously created instances for later.
Here is an example in Swift.
Polymorphism is one of the three key pillars of Object Oriented Programming:
Polymorphism
Abstraction
Inheritance
Being such a key pillar of OOP, to say there the same thing is akin to saying racing is the same thing as driving (driving being the "core underlying thing" that you're doing). This is also like saying the Facade Pattern is simply Abstraction.
Much like racing is driving fast and competitively, the Strategy Pattern is Polymorphism used at runtime to select an algorithm's behavior. It is an application and implementation of Polymorphism, absolutely, but it's really a specific application and implementation of Polymorphism.