Middle+
What design patterns do you use in your practice?
sobes.tech AI
Answer from AI
I use the following design patterns:
Creational Patterns:
- Singleton: Ensures that a class has only one instance and provides a global point of access to it. Used for loggers, configuration managers.
- Factory Method: Defines an interface for creating an object but allows subclasses to decide which class to instantiate. Delegates object creation to subclasses.
- Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Used for working with different sets of related objects, such as creating GUI elements for different operating systems.
- Builder: Allows creating complex objects step by step. Provides a way to obtain different variations of an object using the same construction process. Useful for objects with many optional parameters.
Structural Patterns:
- Adapter: Allows objects with incompatible interfaces to work together. Wraps an existing class with a new interface.
- Decorator: Dynamically adds new responsibilities to an object. A flexible alternative to inheritance for extending functionality.
- Facade: Provides a simple unified interface to a complex subsystem. Simplifies interaction with a set of classes.
Behavioral Patterns:
- Observer: Defines a "one-to-many" dependency between objects so that when one object changes its state, all dependents are notified and updated automatically. Used in UI frameworks, event systems.
- Strategy: Encapsulates family of algorithms, making them interchangeable. Allows selecting an algorithm at runtime.
- Template Method: Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Allows subclasses to override certain steps of the algorithm without changing its structure.
- Iterator: Provides a way to access all elements of a composite object sequentially without exposing its internal representation.
When choosing a pattern, I adhere to SOLID principles and the necessity to solve a specific problem, improving code readability, maintainability, and extensibility. It is also important not to apply patterns unnecessarily (YAGNI - You Aren't Gonna Need It).