d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Strategy Design Pattern

The best way to use design pattern is to load your brain with them and then recognise places in your designs and existing applications where you can apply them.

Prior going for a solution, we should understand the problem by taking an example of Duck game application.

So this blog has two parts
* Problem Part
* Solution Part


Initial Requirement:
Design a Duck game, where a duck can swim and quack.

Problem Part

So as a developer we came with initial design with object oriented approach. Where we have developed a Duck super class which have methods with name swim, quack and display. So that whenever we need to create a new Duck implementation it can inherit super class.

Initial design is okay, and working fine as per business requirement, somehow after 6 months business decided to add new feature flying duck.
So development team creates another method named as fly in super class.

So because of this design, every duck is now flying capabilities which is not correct as per business, and business have decided to add more features in every 6 month release. Thus development team has to change their design, now developer segregate fly and quack code in interface.

Design is okay and flexible, but due to this code is missing the reusability part and each class must have to provide implementation for abstract methods. Thus code maintenance cost will increase and they have take care of each class code, whenever new change requires.

No matter how well you design your application, over time an application must grow, change and creates a design problem.

So now onwards we have to take help of Design Principle and Pattern.

Solution Part

We will use three Design Principle to solve the design problem.

1) Design Principle :
Identify the aspects of your application that vary and separate(encapsulate) them from what stay the same.

2) Design Principle :
Program to interface not an implementation.

3) Design Principle :
Favour composition over inheritance.

Complete Design Solution


Definition :

Strategy Design Pattern defines a family of algorithms, encapsulate each one, and makes them interchangeable.
Strategy let the algorithm vary independently from client.

Add Comment