Strategy pattern implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

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.

Strategy Pattern Class Diagram

Strategy Pattern Class Diagram

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 classLet 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
Bike Example for Strategy Pattern Implementation

Bike Example for Strategy Pattern Implementation

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:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout strategy

If you don’t have git installed, you can either directly download zip or copy paste from below.

Bike.java

RidingStyle.java

RashRiding.java

CarefulRiding.java

Rider.java

If you run Rider class, you will see output

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.

Share Button

3 comments for “Strategy pattern implementation

  1. Nyng
    April 4, 2017 at 8:10 am

    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

    • April 4, 2017 at 10:00 am

      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)

Leave a Reply

Your email address will not be published. Required fields are marked *