fork download
  1. // ==================================================
  2. // Strategy Design Pattern
  3. // ==================================================
  4. //
  5. // What is Strategy Pattern?
  6. // Strategy Pattern lets you define a family of algorithms,
  7. // put each algorithm in its own class, and make them interchangeable.
  8. //
  9. // The client (Context) can switch between different algorithms
  10. // at runtime without changing its own code.
  11. //
  12. // When to use:
  13. // - When you have multiple ways to do the same task
  14. // - When you want to avoid long if-else or switch statements
  15. // - When you want to follow Open-Closed Principle
  16. // (open for extension, closed for modification)
  17. //
  18. // Key parts:
  19. // 1. Strategy Interface → common contract for all algorithms
  20. // 2. Concrete Strategies → different implementations of the algorithm
  21. // 3. Context → uses a Strategy and can switch it at runtime
  22. //
  23. // Example used here: Google Maps path calculation
  24. // Different modes (Car, Bike, Walk) need different path logic.
  25. // Instead of if-else inside GoogleMaps, each mode has its own strategy class.
  26. // ==================================================
  27.  
  28. // ==================================================
  29. // File: PathCalculationStrategy.java
  30. // Strategy Interface
  31. // Defines the common method that all path calculation algorithms must implement
  32. // ==================================================
  33. interface PathCalculationStrategy {
  34. String calculatePath(String from, String to);
  35. }
  36.  
  37. // ==================================================
  38. // File: CarPathStrategy.java
  39. // Concrete Strategy 1: Car
  40. // ==================================================
  41. class CarPathStrategy implements PathCalculationStrategy {
  42. @Override
  43. public String calculatePath(String from, String to) {
  44. // Real logic would use road network, traffic, etc.
  45. return "Car route: " + from + " → " + to + " (via highways, ~45 mins)";
  46. }
  47. }
  48.  
  49. // ==================================================
  50. // File: BikePathStrategy.java
  51. // Concrete Strategy 2: Bike
  52. // ==================================================
  53. class BikePathStrategy implements PathCalculationStrategy {
  54. @Override
  55. public String calculatePath(String from, String to) {
  56. return "Bike route: " + from + " → " + to + " (via bike lanes, ~55 mins)";
  57. }
  58. }
  59.  
  60. // ==================================================
  61. // File: WalkPathStrategy.java
  62. // Concrete Strategy 3: Walk
  63. // ==================================================
  64. class WalkPathStrategy implements PathCalculationStrategy {
  65. @Override
  66. public String calculatePath(String from, String to) {
  67. return "Walking route: " + from + " → " + to + " (via footpaths, ~1 hr 20 mins)";
  68. }
  69. }
  70.  
  71. // ==================================================
  72. // File: GoogleMaps.java
  73. // Context Class
  74. // Holds a reference to the current strategy and delegates the work
  75. // ==================================================
  76. class GoogleMaps {
  77. private PathCalculationStrategy pathStrategy;
  78.  
  79. // Strategy can be changed at runtime
  80. public void setPathStrategy(PathCalculationStrategy strategy) {
  81. this.pathStrategy = strategy;
  82. }
  83.  
  84. public String findPath(String from, String to) {
  85. if (pathStrategy == null) {
  86. throw new IllegalStateException("Path strategy not set");
  87. }
  88. return pathStrategy.calculatePath(from, to);
  89. }
  90. }
  91.  
  92. // ==================================================
  93. // File: StrategyDemo.java
  94. // Client / Usage
  95. // (public removed so it can compile in the same file)
  96. // ==================================================
  97. class StrategyDemo {
  98. public static void main(String[] args) {
  99. GoogleMaps maps = new GoogleMaps();
  100.  
  101. // User selects Car
  102. maps.setPathStrategy(new CarPathStrategy());
  103. System.out.println(maps.findPath("Home", "Office"));
  104.  
  105. // User switches to Walk at runtime
  106. maps.setPathStrategy(new WalkPathStrategy());
  107. System.out.println(maps.findPath("Home", "Park"));
  108.  
  109. // User switches to Bike
  110. maps.setPathStrategy(new BikePathStrategy());
  111. System.out.println(maps.findPath("Home", "Gym"));
  112. }
  113. }
Success #stdin #stdout 0.16s 36996KB
stdin
Standard input is empty
stdout
Car route: Home → Office (via highways, ~45 mins)
Walking route: Home → Park (via footpaths, ~1 hr 20 mins)
Bike route: Home → Gym (via bike lanes, ~55 mins)