Strategy Pattern
Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
lightbulb Analogy
Imagine a navigator app. Whether you choose "Walk", "Drive", or "Public Transit", the destination remains the same, but the route calculation strategy changes. The app context delegates the work to the selected strategy.
play_circle Interactive Demo: Payment Processor
Paying with Credit Card
Processing standard transaction...
thumb_up Advantages
- check_circle Open/Closed Principle: You can introduce new strategies without changing the code of the context.
- check_circle Runtime Switching: Unlike inheritance, strategies can be swapped at runtime (as seen in the demo).
- check_circle Isolation: Implementation details of an algorithm are isolated from the code that uses it.
thumb_down Disadvantages
- cancel Complexity: If you only have a few algorithms that rarely change, the pattern adds unnecessary classes.
- cancel Client Awareness: The client must be aware of the differences between strategies to select the right one.
Observer Pattern
Defines a subscription mechanism to notify multiple objects about any events that happen to the object they're observing.
lightbulb Analogy
Think of a Magazine Subscription. The publisher (Subject) sends new issues to everyone on the list (Observers). If you unsubscribe, you stop receiving them. The publisher doesn't care who you are, just that you are on the list.
play_circle Interactive Demo: Weather Station
Weather Station
The Subject
Active Observers
Mobile App
Updates instantly.
City Billboard
Public display.
Auto-Fan System
Reacts to temp > 25°C.
thumb_up Advantages
- check_circle Loose Coupling: The subject doesn't know the concrete class of the observers, just that they implement the update interface.
- check_circle Dynamic Relationships: Observers can be added or removed at runtime easily.
thumb_down Disadvantages
- cancel Unexpected Updates: Observers might not know why they were updated, leading to expensive cascades if not managed.
- cancel Memory Leaks: The "Lapsed Listener" problem occurs if observers are not explicitly deregistered (they stay in memory).
Visitor Pattern
Lets you separate algorithms from the objects on which they operate. You can add new operations to existing object structures without modifying the structures.
lightbulb Analogy
Consider a Zoo. The animals (Element classes) are distinct: Lion, Dolphin, Eagle. A Vet (Visitor) comes and treats each differently. Later, a Feeder (New Visitor) comes and feeds each differently. The animals don't change, but the operations performed on them do.
play_circle Interactive Demo: Shape Calculator
Object Collection (Shapes)
Select Visitor
thumb_up Advantages
- check_circle Extensibility: You can add new operations (Visitors) without changing the class definitions of the elements.
- check_circle Centralization: Related behavior is gathered into the visitor class instead of being spread across all elements.
thumb_down Disadvantages
- cancel Rigid Elements: It's hard to add new element types (e.g., adding "Triangle") because you have to update all existing Visitors.
- cancel Encapsulation: Visitors might need access to the internal details of elements, breaking encapsulation.