JavaIntermediate#design-patterns

What is the Strategy pattern?

A behavioral pattern that defines a family of interchangeable algorithms, encapsulated behind a common interface, allowing the algorithm to be selected at runtime without modifying the client code.

Example
interface DiscountStrategy { double apply(double price); }
class BlackFridayDiscount implements DiscountStrategy { public double apply(double p) { return p * 0.5; } }
double finalPrice = strategy.apply(100.0);

Related Questions

1
JavaAdvanced#design-patterns

What is the difference between the Adapter and Decorator patterns?

Open
2
JavaAdvanced#design-patterns#oop

What is the difference between composition and inheritance, and why is 'favor composition over inheritance' recommended?

Open
3
JavaAdvanced#design-patterns

What is the Template Method pattern?

Open