Common Patterns in Java
Common patterns include Factory, Builder, Strategy, Adapter, Decorator, Observer, Template Method, Command, and State.
interface PricingStrategy {
long price(Order order);
}
final class RegularPricing implements PricingStrategy {
public long price(Order order) {
return order.subtotal();
}
}
Strategy is polymorphism used to select interchangeable behavior. Builder helps construct complex immutable objects. Adapter translates one interface into another. Decorator adds behavior without modifying the wrapped implementation.
Key Takeaways
Patterns are reusable design ideas, not mandatory class structures.
Strategy uses polymorphism to vary behavior.
Prefer the simplest design that remains easy to change.