Design Patterns Brief
Strategy Pattern
“Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. This pattern lets the algorithm vary independently of the clients that use it”You design a common Strategy interface (for example, CompressionStrategy) and implement multiple concrete strategies (e.g. ZipCompression, RarCompression). A context class holds a reference to a strategy and delegates to it. At runtime the client picks and switches strategies as needed, without changing the context’s code.
Observer Pattern
“Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically”One object (Subject) maintains a list of observers and sends updates to them when its state changes. Observers implement a common interface and register with the subject. When the subject’s state updates (e.g. data model change), it calls notifyObservers(), causing each observer’s update method to run. This decouples subjects and observers.
Decorator Pattern
“Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality” You have a base interface or class (e.g.Beverage) and concrete implementations (Espresso, Tea). Instead of subclassing
every possible combination (e.g. EspressoWithMilk), you create Decorator classes that wrap a Beverage and add
behavior (e.g. MilkDecorator). Multiple decorators can wrap an object at runtime to accumulate behaviors. Each
decorator has the same interface, so it’s transparent to clients.