Strategy design pattern

Chinmay Venkata Sabbam
art of coding
Published in
3 min readMay 21, 2023

Let's Imagine we are in the Car Development Software Company and we are having Car Company initial Design like this

Initial Design of the Software

initial design of the software

we implemented the all features of the car like fuel, engine, tyres and headlamps etc ....

Later Board Members Decided to implement the new feature "Auto gear" which is a highly demandable feature in the market and ask Managers to implement it in the software and come with Demo within one week.

Managers assigned this task to one of the developers to implement the "Auto gear" feature. Developer Said that I am an OOps programmer and I will Implement this feature within an hour and come up with a design like this

Initial Implementation of Auto Gear:

addition of auto gear feature

The manager start giving a demo in front of the Board members and they were surprised that Ambassador and Alto had an Auto gear Feature they decided that we can't afford this feature in these cars and asked the manager to remove the Auto gear feature in these cars.

The manager asked the developer to remove the "Auto gear feature in Ambassador and Alto cars.

Developer thinking that how to remove the Auto gear feature in Ambassador and Alto cars, Since he is an OOps programmer, overriding the isAutoGear() method will solve the problem.

During this Implementation process, Board members decided to implement different kinds of engines in different cars. when engines are different than automatically fuels are also different and ask the manager and developer to implement the same in the software.

The developer taught that specs are changing in every board meeting and he comes with a design like this

Design with Interfaces

with interfaces design

The developer brings this design to the manager to review. The manager asked the Developer, Is it a perfect design to Implement our specs?

what is your taught process on this ..... Is there any drawback to the above design?

Drawbacks of above design

Imagine we have 100 car types Instead of 4 in the future we need to override Engine, Gear and Fuel Interface methods in every class, which causes

  • Duplicate code across sub classes
  • If something changes, we need to change in every class, huge chances to get issues.
  • Maintaining code is very difficult
  • Hard to gain knowledge over Car behaviors and Run time changes are also difficult.

Strategy pattern

Later Developer made a brainstorm and observed what are the specs frequently changing and noted their behaviors and he also observed what are specs not changing in Car and he made a list like this

  • changed specs :
  1. engine
  2. fuel
  3. gear
  • unchanged specs :
  1. tyres
  2. headlamps

and he encapsulated the changed specs in the Car class and made a final design like this.

strategy pattern design

"Observed the behavior of the class and Encapsulate what is changing frequently. This pattern is called strategy pattern"

Object-oriented basics covered in this pattern

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

Object-oriented principles we followed in this pattern

  • Encapsulate what varies
  • Favorable Composition Over inheritance
  • Program to interfaces, not for Implementations
//encapsulation and abstraction and Program to interfaces
private Engine engine ;
private Fuel fuel;
private Gear gear;

// polymorphism
Engine engine = new SimplePetrolEngine();
setEngine(engine);
Fuel fuel = new Petrol();
setFuel(fuel);
Gear gear = new NormalGear();
setGear(gear);

--

--