Strategy Design Pattern
If you have multiple conditions (equivalent to multiple if-else statements) within a switch statement and you want to refactor them using the strategy pattern, you can create a strategy for each condition. Let's use an example of a system that calculates shipping costs based on the destination and weight of a package.
public class ShippingCalculator { public double calculateShippingCost(String destination, double weight) { double cost; switch (destination) { case "Local": if (weight <= 5) { cost = 5.0; } else { cost = 8.0; } break; case "Domestic": if (weight <= 5) { cost = 10.0; } else { cost = 15.0; } break; case "International": if (weight <= 5) { cost = 20.0; } else { cost = 30.0; } break; default: throw new IllegalArgumentException("Invalid destination: " + destination); } return cost; } }
Refactored Code using Strategy Pattern:
Step 1: Define the ShippingStrategy Interface
Step 2: Create Concrete Strategy Classes
Step 3: Modify the ShippingCalculator to Use Strategy.
Step 4: Usage
ShippingCalculator
initially uses a switch statement with multiple conditions. The refactored code introduces a ShippingStrategy
interface and concrete strategy classes (LocalShippingStrategy
, DomesticShippingStrategy
, InternationalShippingStrategy
). The ShippingCalculator
class is modified to use the strategy pattern, allowing you to switch between shipping strategies dynamically without modifying the ShippingCalculator
class. This approach makes the code more modular, extensible, and easier to maintain.
Comments
Post a Comment