top of page

Strategy Design Pattern

The Strategy design pattern is a behavioural design pattern that allows you to define a family of algorithms or behaviours, encapsulate each one of them, and make them interchangeable. It enables a client to choose the algorithm to use at runtime without tightly coupling the client to a specific implementation. This pattern is useful when you have multiple algorithms that perform similar tasks but have different implementations or variations.


For example let's suppose we have different type of vehicles and based on the vehicle we want to print the vehicle features, then how can we handle using design pattern


for above example first let's create a interface Strategy

public interface Strategy {
   public void drive();
   public void printSpecifications();
}

after that need to implement these methods in strategy class

let's suppose we have to two strategy class


1. SportVehicleStrategy

public class SportVehicleStrategy implements Strategy{
   @Override
   public void drive() {
      System.out.println("this is a sport vehicle strategy");
   }

   @Override
   public void printSpecifications() {
      String vehicleName = "Sport";
      int maxSpeed = 200;
      int gears = 6;
      String mileage = "30km";
      System.out.println(vehicleName + " , " + maxSpeed + " , " + gears + " , " + mileage);
   }
}

2. NormalVehicleStrategy

public class NormalVehicleStrategy implements Strategy{
   @Override
   public void drive() {
      System.out.println("this is a normal vehicle strategy");
   }
   @Override
   public void printSpecifications() {
      String vehicleName = "Normal";
      int maxSpeed = 80;
      int gears = 4;
      String mileage = "60km";
      System.out.println(vehicleName + " , " + maxSpeed + " , " + gears + " , " + mileage);
   }
}

Now let's create base vehicle class which has a strategy



public class Vehicle {
   private final Strategy strategy;
   public Vehicle(Strategy strategy){
      this.strategy = strategy;
   }
   public void drive(){
      strategy.drive();
   }

   public void print(){
      strategy.printSpecifications();
   }
}
   

And let's suppose we have to vehicle


1. SportVehicle

public class SportVehicle extends Vehicle{
   public SportVehicle() {
      super(new SportVehicleStrategy());
   }
}

2. NormalVehicle

public class NormalVehicle extends Vehicle{
   public NormalVehicle() {
      super(new NormalVehicleStrategy());
   }
}

Test Strategy pattern


public class Main {
   public static void main(String[] arr){
      testStrategyPattern();
   }
   
   public static void testStrategyPattern(){
     SportVehicle sportVehicle = new SportVehicle();
     sportVehicle.print();
     NormalVehicle normalVehicle = new NormalVehicle();
     normalVehicle.print();
   }
}

Output:

Sport , 200 , 6 , 30km
Normal , 80 , 4 , 60km

Recent Posts

See All

ความคิดเห็น


Call 

7869617359

Email 

Follow

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
Never Miss a Post. Subscribe Now!

Thanks for submitting!

bottom of page