Strategy Design Pattern

Himanshu verma
3 min readFeb 8, 2023

--

What is Design Pattern ?

“Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development.”

So you might got an idea about what is Design pattern now :)

Let’s begin with Strategy Design Pattern now,

In strategy design pattern , a class behaviour or its algorithm can be changed at run time .

Didn't get it ,right :(

Further Explanation :-
We create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

In layman terms :- “Suppose we have a class (Parent class) and it has four different child classes, and in two of the child class we have same work going on in one of their function , so there is duplication of code, we are doing unnecessary work

Let me explain it to you with an example :-

/**
* Main Class
*/
open class Bike {
open fun engineFeatures()
{
//TODO we have 4 gears only
}
}
/**
*
* Subclasses
*
*/
class SportyBike :Bike() {
@Override
override fun engineFeatures()
{
//TODO we have 4 gears with alloy wheels
}
}
class MonsterBike : Bike()
{
@Override
override fun engineFeatures() {
// TODO we have 4 gears with alloy wheels
}
}
class LongRouteBike :Bike()
{
@Override
override fun engineFeatures() {
super.engineFeatures()
}
}
class CityBike :Bike()
{
@Override
override fun engineFeatures() {
//TODO we have 5 gears with alloy wheels
}
}

As we see the top two bike class require different EngineFeatures but last bike class is using the base class enginefeatures . And both the classes SportyBike and MonsterBike are having same engineFeatures code , so there is duplication in the code, why we are writing the same code in different classes, it is not scalable too.

Let’s look at solution :-

interface EngineFeatureStrategy{
fun engineFeatures()
}

class EasyFeatures : EngineFeatureStrategy{
override fun engineFeatures() {
// TODO we will have 4 gears
}
}
class SportyFeatures :EngineFeatureStrategy{
override fun engineFeatures() {
//TODO we will have 4 gears with alloy wheels
}
}
open class BikeModified{
var obj : EngineFeatureStrategy
// constructor injection
constructor(obj:EngineFeatureStrategy)
{
this.obj = obj
}
open fun engineFeatures()
{
obj.engineFeatures()
}
}
class SportyBikeModified :BikeModified(SportyFeatures())
class MonsterBikeModified :BikeModified(SportyFeatures())
class LongRouteBikeModified :BikeModified(EasyFeatures())
class CityBikeModified :BikeModified(EasyFeatures())
/**
*
* And to run it you can simply do this
*
*/
SportyBikeModified().engineFeatures()

So what we did is , we just divided the different work into different classes(EasyFeatures ,SportyFeatures) , so that which ever class (SportyBikeModified/CityBikeModified) need different feature can easily use this class.

We also made of SOLID principle’s last method Dependency Injection(construction injection ) in BikeModified class, so that we have scalable EngineFeatureStrategy (meaning not fixed to one i.e EasyFeatures/SportyFeatures).

Hope you got somewhat a brief idea about Strategy Design Pattern.

References :-https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm

In case of doubt , ASK IT OUT :-https://www.linkedin.com/in/himanshu-verma-318903171/

--

--