Close
Strategy Pattern for SuperHero powers

Strategy design pattern

Introduction

Strategy design pattern states that if an algorithm has different forms. Then each of these forms must be implemented in their own class. The client using these different algorithmic forms is allowed to interchangeable use each of this as required.

Why do we need this strategy pattern?

Lets assume that we have a module which takes care of payment of the selected items in the cart.

Initially the checkout module only supported payment via debit/credit cards. All the clients have been happy for a year from the release.

After sometime, the clients asked for new feature in the payment module to support payment via paypal. Since the payment module is standalone and things are strongly coupled. Adding support for paypal is challenging, but somehow we managed to add this. Clients are happy :), but now it is difficult for the developers to maintain.

After a year and a half, a new way of payment has emerged called UPI which was much more convenient. Clients asked for payment via UPI as well. Now the current architecture is so complex, adding a new payment type would be more difficult.

Do you see the problem here in the current approach? The payment types are coupled, adding new types are difficult and are not easily exchangeable as needed.

This exact issue strategy pattern tries to solve. Now let’s discuss the pattern now.

Strategy pattern Internals

The strategy pattern needs two components

  • Context class – This keeps the handle current strategy in use.
  • Strategy Interface – This is a common interface which each algorithmic form has to implement.

Clients can also take care of handling algorithmic forms which are being handled in that context class. In that case context class may not be needed. But it is advisable to use the context class added security layer and decoupled module.

Strategy pattern internals

Strategy pattern with Example

Let’s take a simple example of the Game Super Hero. This super hero has the following powers.

  • Fly
  • Swim Fast
  • Attack with fire

Functional requirements for SuperHero

  • The game should support a new super hero(Profession X) that should have Fly, Fast Swim, Attack with Fire powers.
  • This new Super hero should be interchangeably use the powers it supports.
Strategy explanation with Super Hero example.

Implementation in Java

Main.java

import working.example.tech.DesignPattern.Behavioural.StrategyPattern.*;
public class Main {
    public static void main(String[] args) {

    	SuperHero hero = new SuperHero();
    	hero.setSuperPowerStrategy(new FlySuperPower());
    	hero.showPower();
    	hero.setSuperPowerStrategy(new FireSuperPower());
    	hero.showPower();
    	hero.setSuperPowerStrategy(new SwinUnderWaterSuperPower());
    	hero.showPower();
          	 
    }
}

SuperHero.java

package working.example.tech.DesignPattern.Behavioural.StrategyPattern;

public class SuperHero {
	SuperPowerStratergy mAction;
	final String mName = "Professor X";
	public SuperHero() {
	}
	
	public void setSuperPowerStrategy(SuperPowerStratergy act) {
		mAction = act;
	}
	
	public void showPower() {
		System.out.print(mName + " ");
		mAction.power();
	}
}

SuperPowerStratergy.java

package working.example.tech.DesignPattern.Behavioural.StrategyPattern;
public interface SuperPowerStratergy {
    void power();
}

FireSuperPower.java

package working.example.tech.DesignPattern.Behavioural.StrategyPattern;

public class FireSuperPower implements SuperPowerStratergy {

	@Override
	public void power() {
		System.out.println("Fire out...");
	}
}

FlySuperPower.java

package working.example.tech.DesignPattern.Behavioural.StrategyPattern;

public class FlySuperPower implements SuperPowerStratergy {

	@Override
	public void power() {
		System.out.println("Flying now...");
	}
}

SwinUnderWaterSuperPower.java

package working.example.tech.DesignPattern.Behavioural.StrategyPattern;

public class SwinUnderWaterSuperPower implements SuperPowerStratergy {
	@Override
	public void power() {
		System.out.println("Swimming fast...");
	}

}

Output

Professor X Flying now...
Professor X Fire out...
Professor X Swimming fast...

Conclusion

This blog covers the basics of how one should use a strategy design pattern with an easy to understand example. Please check the Vending Machine LLD to get understanding on strategy design pattern usage. How Vending Machine uses the Money type scanner strategy effectively via this pattern.

More blogs

State Design Pattern

Design a Vending Machine LLD

How to write a webcrawler for Large Language Models at scale (Part – 3)