Latest posts by Prasad Kharkar (see all)
- How do neural networks learn? - April 17, 2018
- Artificial Neural Networks - April 17, 2018
- The Neuron in Deep Learning - April 17, 2018
Continuing with our design patterns tutorials, we will look into another design pattern. We are going to study strategy pattern implementation. I feel this is simpler as compared to other patterns such as factory method and abstract factory implementation.
Strategy Pattern Implementation:
Strategy pattern is defined as,
A family of algorithms encapsulate each one and makes them interchangeable. Strategy lets an algorithm vary independently from clients that use it.
Generally we associate behaviour with an object and define it in class itself. When we define ride() method in Bike class, we are implementing riding behaviour for bike there itself. What if we want to change riding behaviour later? What if the we want to keep riding behaviour separate from Bike?
We can delegate this behaviour to another class and take help of that class in Bike. i.e. we can separate Behaviour from object itself. Strategy Pattern Implementation takes following form.
Main components of Strategy Pattern Implementation are:
- Context: contains a reference to strategy object and receives requests from the client, which request is then delegated to strategy.
- Strategy: Actual behaviour that we have delegated in strategy pattern implementation. This defines an interface common to all supported behaviours.
- ConcreteStrategy: Each concrete strategy implements a behavior.
Let us take an example of Bike and bike riding style. We have few assumptions.
- A Bike is ridden in various ways.i.e. it can have multiple behaviours. It acts as Context
- As per definition we want to separate bike behaviour from Bike class, Let us create an interface which denotes all behaviours. Make interface RidingStyle, all behaviours will implement this. RidingStyle acts as Behaviour
- CarefulRiding and RashRiding implement RidingStyle. Both act as ConcreteBehaviour.
- Bike will have association with RidingStyle, with this, Bike is delegating its behaviour to RidingStyle
Let us try design pattern implementation with example.
If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.
Go to your working directory and then follow below steps:
- git clone https://github.com/theJavaGeek/design-patterns.
- cd design-patterns
- git checkout strategy
If you don’t have git installed, you can either directly download zip or copy paste from below.
Bike.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.thejavageek.designpatterns; public class Bike { private RidingStyle ridingStyle; public Bike() { ridingStyle = new CarefulRiding(); } public RidingStyle getRidingStyle() { return ridingStyle; } public void setRidingStyle(RidingStyle ridingStyle) { this.ridingStyle = ridingStyle; } public void rideBike() { ridingStyle.ride(); } } |
RidingStyle.java
1 2 3 4 5 |
package com.thejavageek.designpatterns; public interface RidingStyle { public abstract void ride(); } |
RashRiding.java
1 2 3 4 5 6 7 8 9 10 |
package com.thejavageek.designpatterns; public class RashRiding implements RidingStyle { @Override public void ride() { System.out.println("Riding like hell, careless and dangerous"); } } |
CarefulRiding.java
1 2 3 4 5 6 7 8 9 10 |
package com.thejavageek.designpatterns; public class CarefulRiding implements RidingStyle { @Override public void ride() { System.out.println("someone is waiting at home, so riding very carefully"); } } |
Rider.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.thejavageek.designpatterns; public class Rider { public static void main(String[] args) { Bike bike = new Bike(); bike.rideBike(); bike.setRidingStyle(new RashRiding()); bike.rideBike(); } } |
If you run Rider class, you will see output
1 2 |
someone is waiting at home, so riding very carefully Riding like hell, careless and dangerous |
Points to note in this strategy pattern implementation:
- Bike class HAS-A RidingStyle i.e. Bike class declares an instance variable of type RidingStyle, which can refer to any subclass.
- Bike class constructor instantiates CarefulRiding by default. i.e. default behaviour of bike is CarefulRiding.
- It also provide a setter method i.e. setRidingStyle(), which is used for externally setting different behaviour.
- For same bike we can assign new riding style by bike.setRidingStyle(RashRiding()), i.e. behaviour is interchangeable.
I hope the article helped understand strategy pattern implementation.
You should be proud of yourself, this is the best site to learn classic design patterns I’ve ever found. IMOO your explanation is even more concise than the book *Head First Design Patterns*. I’m wondering your stack overflow id @Prasad Kharkar
Hi Nyng, I am glad you liked my blogs about design patterns. I learned from various books and blogs all over the internet and tried to understand every design pattern thoroughly and then I wrote articles about them. My objective has always been to explain things in concise manner. I think you are the first one to comment on my articles about design patterns and it has definitely encouraged me 🙂 . This is my stackoverflow profile : http://stackoverflow.com/users/1727645/prasad-kharkar . (I am not quite active nowadays though)
This is the best design pattern learning stuff i have ever read on internet…
Thank you Rachit, I have spent a lot of time studying, implementing and writing these design pattern articles and your words mean so much to me. I hope these design pattern articles reached to most people