Open In App

Strategy Design Pattern Example and Implementation

In our previous discussion Strategy Design Pattern, we explored the Strategy Design Pattern, a powerful tool in software design. We learned what it is, its key features, and its components. We also looked at real-world examples and discussed when it’s useful and when it might not be the best choice. Now we’ll use a new example to see how this pattern works in action. By breaking down the implementation step by step, we’ll make it easier to understand how to apply this pattern effectively.

Strategy Design Pattern Example

In a transportation system, users need to travel from one location to another using different modes of transportation, such as cars, bicycles, and walking. The system should support dynamic selection and switching between these travel modes based on user preferences or travel requirements.



Challenges Without Using Strategy Pattern

How Strategy Pattern helps to solve the above challenges

Complete Code of the above example

1. Define the Strategy Interface

We start by defining the “TravelStrategy" interface, which declares a method “travel" for traveling from one location to another.






interface TravelStrategy {
    void travel(String source, String destination);
}

2. Implement Concrete Strategies

We then create concrete strategy classes implementing the “TravelStrategy" interface for each travel mode: “CarTravelStrategy", “BicycleTravelStrategy", and “WalkingTravelStrategy".




class CarTravelStrategy implements TravelStrategy {
    @Override
    public void travel(String source, String destination) {
        System.out.println("Traveling by car from " + source + " to " + destination);
    }
}
 
class BicycleTravelStrategy implements TravelStrategy {
    @Override
    public void travel(String source, String destination) {
        System.out.println("Traveling by bicycle from " + source + " to " + destination);
    }
}
 
class WalkingTravelStrategy implements TravelStrategy {
    @Override
    public void travel(String source, String destination) {
        System.out.println("Traveling on foot from " + source + " to " + destination);
    }
}

3. Create the Context Class

We define a context class “TravelPlanner", which has a reference to the “TravelStrategy" interface. This class will utilize the selected travel strategy to perform travel operations.




class TravelPlanner {
    private TravelStrategy travelStrategy;
 
    public TravelPlanner(TravelStrategy travelStrategy) {
        this.travelStrategy = travelStrategy;
    }
 
    public void setTravelStrategy(TravelStrategy travelStrategy) {
        this.travelStrategy = travelStrategy;
    }
 
    public void planTravel(String source, String destination) {
        travelStrategy.travel(source, destination);
    }
}

4. Client Code

Finally, in the client code, we instantiate the “TravelPlanner" object and specify the desired travel mode dynamically.




public class Client {
    public static void main(String[] args) {
        TravelPlanner travelPlanner = new TravelPlanner(new CarTravelStrategy());
 
        // Plan travel by car
        travelPlanner.planTravel("Home", "Office");
 
        // Change strategy to bicycle
        travelPlanner.setTravelStrategy(new BicycleTravelStrategy());
        // Plan travel by bicycle
        travelPlanner.planTravel("Office", "Park");
 
        // Change strategy to walking
        travelPlanner.setTravelStrategy(new WalkingTravelStrategy());
        // Plan travel by walking
        travelPlanner.planTravel("Park", "Restaurant");
    }
}

Complete code for the above example

Below is the complete code for the above example:




// TravelStrategy.java
interface TravelStrategy {
    void travel(String source, String destination);
}
 
// CarTravelStrategy.java
class CarTravelStrategy implements TravelStrategy {
    @Override
    public void travel(String source, String destination) {
        System.out.println("Traveling by car from " + source + " to " + destination);
    }
}
 
// BicycleTravelStrategy.java
class BicycleTravelStrategy implements TravelStrategy {
    @Override
    public void travel(String source, String destination) {
        System.out.println("Traveling by bicycle from " + source + " to " + destination);
    }
}
 
// WalkingTravelStrategy.java
class WalkingTravelStrategy implements TravelStrategy {
    @Override
    public void travel(String source, String destination) {
        System.out.println("Traveling on foot from " + source + " to " + destination);
    }
}
 
// TravelPlanner.java
class TravelPlanner {
    private TravelStrategy travelStrategy;
 
    public TravelPlanner(TravelStrategy travelStrategy) {
        this.travelStrategy = travelStrategy;
    }
 
    public void setTravelStrategy(TravelStrategy travelStrategy) {
        this.travelStrategy = travelStrategy;
    }
 
    public void planTravel(String source, String destination) {
        travelStrategy.travel(source, destination);
    }
}
 
// Client.java
public class Client {
    public static void main(String[] args) {
        TravelPlanner travelPlanner = new TravelPlanner(new CarTravelStrategy());
 
        // Plan travel by car
        travelPlanner.planTravel("Home", "Office");
 
        // Change strategy to bicycle
        travelPlanner.setTravelStrategy(new BicycleTravelStrategy());
        // Plan travel by bicycle
        travelPlanner.planTravel("Office", "Park");
 
        // Change strategy to walking
        travelPlanner.setTravelStrategy(new WalkingTravelStrategy());
        // Plan travel by walking
        travelPlanner.planTravel("Park", "Restaurant");
    }
}




Traveling by car from Home to Office
Traveling by bicycle from Office to Park
Traveling on foot from Park to Restaurant

Conclusion

In this example, the Strategy Design Pattern allows us to dynamically select and switch between different travel modes (CarTravelStrategy, BicycleTravelStrategy, WalkingTravelStrategy) without modifying the TravelPlanner class. This provides flexibility and extensibility to the transportation system, enabling it to accommodate various travel preferences and scenarios.


Article Tags :