Strategy Design Pattern

When to use ?

Consider a scenario where different child classes inherited from superclass might have same method implementation , so we see a code duplication in child classes. To avoid this we come up with strategy design pattern.

public interface DriveStrategy() {
    public void drive();
}
//Drive Strategy implementations.
public class NormalDriveStrategy implements DriveStrategy() {
    @Override
    public void drive() {
        System.out.println("NOrmal drive strategy");    
    }
}

public class SportDriveStrategy implements DriveStrategy() {
      public void drive() { 
        System.out.println("SPort drive strategy");        
      }    
}
public class Vehicle{
  DriveStrategy driveobj;

  Vehicle(DriveStrategy driveobj) {
    this.driveobj = driveobj;    
}
public void drive() {
    driveobj.drive();    
}
}

//Child classes 
public class offroadVehicle extends Vehicle { 
       public offroadVehicle() { 
            super(new NormalDriveStrategy());
        }
}

public class sportVehicle extends Vehicle { 
        public sportVehicle() { 
            super(new SportDriveStrategy());    
        }
}

//Main method
public class void main(String args[]) {
    Vehicle vehicle = new sportVehicle();
     vehicle.drive();
}