My vision and realization GOF Patterns
- Singleton - allows you to create only one instance of an object.
Example: only one Database connection. - Factory Method - allows creating different instances of objects using a dictionary, avoiding IF statements.
Example: GET, POST, PUT, DELETE handlers. - Abstract Factory - allows creating a factory of factories.
Example: Factory of Factories. - Prototype - creates new objects by copying an existing object (deep copy).
Example: base Docker container. - Builder - allows creating complex objects part by part.
Example: building an SQL query (Base, Filters, Limits, Sorts).
- Adapter - allows incompatible interfaces to work together.
Example: connecting Internal and External APIs. - Bridge - helps connect one interface to another.
Example: connecting SQL and NoSQL databases through a bridge. - Facade - provides a simplified interface to a complex system.
Example: Interface to all Microservices. - Composite - helps to work with objects and nested objects of the same type.
Example: Module manager with a hierarchical structure. - Decorator - extends the functionality of an object.
Example: Python decorator. - Flyweight - helps use less memory and time by sharing as much data as possible.
Example: caching strategy in backend systems. - Proxy - adds a level of abstraction before accessing an object.
Example: like VPN or Authorization.
- Memento - Captures and restores an object's internal state.
Example: GIT snapshot of a repository. - Command - Encapsulates a request as an object.
Example: Git revert. - Interpreter - Defines a grammatical representation for a language and an interpreter to interpret the grammar.
Example: SQL or regular expression parsing. - Chain of Responsibility - Passes a request along a chain of handlers.
Example: Backend API request processing pipeline. - Iterator - Provides a way to access elements of a collection.
Example: Iterating over a collection. - Mediator - Defines an object that encapsulates how a set of objects interact.
Example: Middleware. - Observer - Notifies dependents when an object changes state.
Example: Notification system. - State - Allows an object to alter its behavior when its internal state changes.
Example: Status of an order with different methods (New, Sent, Delivered). - Strategy - Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Example: Choosing a caching strategy (Memory or Disk). - Template Method - Defines the skeleton of an algorithm, deferring some steps to subclasses.
Example: Different payment methods (Stripe, PayPal) with different specifics but a common structure (validate/pay/log). - Visitor - Allows new operations to be defined without changing the classes of the elements on which it operates.
Example: Logging system.