// ==================================================
// Strategy Design Pattern
// ==================================================
//
// What is Strategy Pattern?
// Strategy Pattern lets you define a family of algorithms,
// put each algorithm in its own class, and make them interchangeable.
//
// The client (Context) can switch between different algorithms
// at runtime without changing its own code.
//
// When to use:
// - When you have multiple ways to do the same task
// - When you want to avoid long if-else or switch statements
// - When you want to follow Open-Closed Principle
// (open for extension, closed for modification)
//
// Key parts:
// 1. Strategy Interface → common contract for all algorithms
// 2. Concrete Strategies → different implementations of the algorithm
// 3. Context → uses a Strategy and can switch it at runtime
//
// Example used here: Google Maps path calculation
// Different modes (Car, Bike, Walk) need different path logic.
// Instead of if-else inside GoogleMaps, each mode has its own strategy class.
// ==================================================
// ==================================================
// File: PathCalculationStrategy.java
// Strategy Interface
// Defines the common method that all path calculation algorithms must implement
// ==================================================
interface PathCalculationStrategy {
}
// ==================================================
// File: CarPathStrategy.java
// Concrete Strategy 1: Car
// ==================================================
class CarPathStrategy implements PathCalculationStrategy {
@Override
// Real logic would use road network, traffic, etc.
return "Car route: " + from + " → " + to + " (via highways, ~45 mins)";
}
}
// ==================================================
// File: BikePathStrategy.java
// Concrete Strategy 2: Bike
// ==================================================
class BikePathStrategy implements PathCalculationStrategy {
@Override
return "Bike route: " + from + " → " + to + " (via bike lanes, ~55 mins)";
}
}
// ==================================================
// File: WalkPathStrategy.java
// Concrete Strategy 3: Walk
// ==================================================
class WalkPathStrategy implements PathCalculationStrategy {
@Override
return "Walking route: " + from + " → " + to + " (via footpaths, ~1 hr 20 mins)";
}
}
// ==================================================
// File: GoogleMaps.java
// Context Class
// Holds a reference to the current strategy and delegates the work
// ==================================================
class GoogleMaps {
private PathCalculationStrategy pathStrategy;
// Strategy can be changed at runtime
public void setPathStrategy(PathCalculationStrategy strategy) {
this.pathStrategy = strategy;
}
if (pathStrategy == null) {
}
return pathStrategy.calculatePath(from, to);
}
}
// ==================================================
// File: StrategyDemo.java
// Client / Usage
// (public removed so it can compile in the same file)
// ==================================================
class StrategyDemo {
public static void main
(String[] args
) { GoogleMaps maps = new GoogleMaps();
// User selects Car
maps.setPathStrategy(new CarPathStrategy());
System.
out.
println(maps.
findPath("Home",
"Office"));
// User switches to Walk at runtime
maps.setPathStrategy(new WalkPathStrategy());
System.
out.
println(maps.
findPath("Home",
"Park"));
// User switches to Bike
maps.setPathStrategy(new BikePathStrategy());
System.
out.
println(maps.
findPath("Home",
"Gym")); }
}