Middle
What design pattern have you used in your projects or code?.
sobes.tech AI
Answer from AI
Depending on the context and tasks, I used various patterns:
-
Creational:
- Singleton (for database connection managers or loggers).
- Factory Method / Abstract Factory (for creating objects of different types, e.g., parsers of various formats).
- Builder (for creating complex objects, such as SQL queries).
-
Structural:
- Adapter (for integration with external APIs or libraries).
- Decorator (for adding functionality to existing objects, e.g., logging or metrics for HTTP handlers).
- Composite (for representing hierarchical structures, e.g., UI elements or AST nodes).
- Facade (to simplify the interface to a complex subsystem).
-
Behavioral:
- Observer (for implementing event subscription).
- Strategy (for selecting an algorithm depending on the context).
- Template Method (for defining the skeleton of an algorithm, allowing subclasses to override certain steps).
- State (for managing the behavior of an object depending on its state).
- Command (to encapsulate a request in an object, allowing clients to parameterize requests, queue them, or log them).
When choosing a pattern, I always try to adhere to the principles of KISS (Keep It Simple, Stupid) and YAGNI (You Ain't Gonna Need It), avoiding premature architecture complexity. In Golang, many patterns are naturally implemented thanks to language features such as interfaces and concurrency.