Introduction
- Overview (GoF design pattern)
- Creational Patterns - object creation
- Singleton : Ensure a class has only one instance ( e.g. config manager, logger)
- Factory Method : Define an interface for creating objects, let subclasses decide.
- Abstract Factory : Provide an interface to create famailies of related objects
- Builder : Step-by-step object construction
- Prototype : Clone objects instead of creating new ones from scratch
- Structual Patterns - object composition
- Adapter : Convert one interface to another
- Bridge : Separate abstraction from implementation
- Composite : Represent part-whole hierarchies (tree structures)
- Decorator : Add behavior to objects dynamically
- Facade : Simplified interface to a complex system
- Flyweight : Optimize memory by sharing objects
- Proxy : Control access to object
- Behavioral Patterns - communication
- Chain of Responsibility : Pass request through a chain until hanled
- Command : Encapsulate requests as objects
- Interpreter : Define grammar and interpret sentences
- Iterator : Sequentially access elements of a collection
- Mediator : Centralize communication between objects
- Memento : Capture and restore an obejct’s state
- Observer : One-to-many dependency
- State : Allow object behavior to change with internal state
- Strategy : Define a family of algorithms, interchangeable at runtime
- Template Method : Define algorithm skeleton, let subclasses fill steps
- Visitor : Separate algorithm from the object structure
- Modern/Enterprise Patterns (GoF extensions)
- Dependency Injection : Invert control of dependencies
- Repository Pattern : Abstract database access
- Model View Controller (MVC) : Separate UI, logic, and data
- Publish-Subscribe (Pub/Sub) : Decouple event producers and consumers
Decorator
- Concepts: a structural design pattern that allows behavior to be added to individual objects dynamically without modifying their class
- It wraps an existing object inside another object that implements the same interfacce and extends its behavior
- Characteristics:
- Adds functionality at runtime, not via inheritance
- Each decorator implements the same interface as the object it decorates
- Can be stacked or combined, where multiple decorators wrap the same base object
- Promotes the Open/Closed Principle - open for extension, closed for modification.
- May lead to complex object chains if overused.
- Usage:
- GUI components (adding scrollbars, borders, etc.)
- I/O stream handling (e.g., Java’s BufferedReader, DataInputStream)
- Middleware or request handlers (adding logging, auth, etc.)
Publish-Subscribe (Pub/Sub)
- Concepts :
- Messaging pattern where senders (publishers) don’t send message directly to receivers (subscribers).
- Instead, messages go through a brocker (or topic)
- Characteristics:
- Publishers publish messages to a topic
- Subscribers subscribe to a topic to receive relevant messages
- Publishers and subscribers are decoupled - they don’t need to know about each other
- Usage
- useful for scalable, event-driven systems liek real-time notifications, logging, streaming data pipelines
- Implementations :
- Kafka, Redis Pub/Sub, Google Pub/Sub