Junior — Middle
Which of the 23 classic patterns described in the book 'Design Patterns: Elements of Reusable Object-Oriented Software' have you applied in your projects?
sobes.tech AI
Answer from AI
In projects, I often use the following patterns from the classic 23:
- Singleton — to ensure a single instance of a class, for example, for a logger.
- Factory Method — to create objects without binding to specific classes.
- Observer — to implement event subscription, for example, in UI.
- Strategy — to choose an algorithm at runtime.
- Decorator — to dynamically extend the functionality of objects.
Example of using the Singleton pattern in C#:
public sealed class Logger
{
private static readonly Logger instance = new Logger();
private Logger() { }
public static Logger Instance => instance;
public void Log(string message) {
Console.WriteLine(message);
}
}