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. // Folder Structure (if kept as separate files):
  29. //
  30. // strategy/
  31. // ├── PathCalculationStrategy.java
  32. // ├── CarPathStrategy.java
  33. // ├── BikePathStrategy.java
  34. // ├── WalkPathStrategy.java
  35. // ├── GoogleMaps.java
  36. // └── StrategyDemo.java
  37. // ==================================================
  38.  
  39. // ==================================================
  40. // File: PathCalculationStrategy.java
  41. // Strategy Interface
  42. // Defines the common method that all path calculation algorithms must implement
  43. // ==================================================
  44. interface PathCalculationStrategy {
  45. String calculatePath(String from, String to);
  46. }
  47.  
  48. // ==================================================
  49. // File: CarPathStrategy.java
  50. // Concrete Strategy 1: Car
  51. // ==================================================
  52. class CarPathStrategy implements PathCalculationStrategy {
  53. @Override
  54. public String calculatePath(String from, String to) {
  55. // Real logic would use road network, traffic, etc.
  56. return "Car route: " + from + " → " + to + " (via highways, ~45 mins)";
  57. }
  58. }
  59.  
  60. // ==================================================
  61. // File: BikePathStrategy.java
  62. // Concrete Strategy 2: Bike
  63. // ==================================================
  64. class BikePathStrategy implements PathCalculationStrategy {
  65. @Override
  66. public String calculatePath(String from, String to) {
  67. return "Bike route: " + from + " → " + to + " (via bike lanes, ~55 mins)";
  68. }
  69. }
  70.  
  71. // ==================================================
  72. // File: WalkPathStrategy.java
  73. // Concrete Strategy 3: Walk
  74. // ==================================================
  75. class WalkPathStrategy implements PathCalculationStrategy {
  76. @Override
  77. public String calculatePath(String from, String to) {
  78. return "Walking route: " + from + " → " + to + " (via footpaths, ~1 hr 20 mins)";
  79. }
  80. }
  81.  
  82. // ==================================================
  83. // File: GoogleMaps.java
  84. // Context Class
  85. // Holds a reference to the current strategy and delegates the work
  86. // ==================================================
  87. class GoogleMaps {
  88. private PathCalculationStrategy pathStrategy;
  89.  
  90. // Strategy can be changed at runtime
  91. public void setPathStrategy(PathCalculationStrategy strategy) {
  92. this.pathStrategy = strategy;
  93. }
  94.  
  95. public String findPath(String from, String to) {
  96. if (pathStrategy == null) {
  97. throw new IllegalStateException("Path strategy not set");
  98. }
  99. return pathStrategy.calculatePath(from, to);
  100. }
  101. }
  102.  
  103. // ==================================================
  104. // File: StrategyDemo.java
  105. // Client / Usage
  106. // (public removed so it can compile in the same file)
  107. // ==================================================
  108. class StrategyDemo {
  109. public static void main(String[] args) {
  110. GoogleMaps maps = new GoogleMaps();
  111.  
  112. // User selects Car
  113. maps.setPathStrategy(new CarPathStrategy());
  114. System.out.println(maps.findPath("Home", "Office"));
  115.  
  116. // User switches to Walk at runtime
  117. maps.setPathStrategy(new WalkPathStrategy());
  118. System.out.println(maps.findPath("Home", "Park"));
  119.  
  120. // User switches to Bike
  121. maps.setPathStrategy(new BikePathStrategy());
  122. System.out.println(maps.findPath("Home", "Gym"));
  123. }
  124. }
Success #stdin #stdout 0.14s 37072KB
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)