Sobes.tech
Junior — Middle

What design patterns do you apply in your practice?

sobes.tech AI

Answer from AI

In practice, I often use the following design patterns in C#:

  • Singleton — for creating a single instance of a class, for example, for a logger or configuration manager.
  • Factory Method — for creating objects without specifying the exact class, convenient when working with class hierarchies.
  • Repository — for abstracting data access, simplifies testing and maintenance.
  • Dependency Injection — for managing dependencies and increasing testability.
  • Observer — for implementing an event-driven model, for example, through delegates and events.

Example of Singleton:

public class Logger
{
    private static Logger _instance;
    private Logger() { }

    public static Logger Instance => _instance ??= new Logger();

    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

The choice of pattern depends on the task and the architecture of the project.