Junior — Middle
What design patterns have you applied in your development practice?
sobes.tech AI
Answer from AI
In my C# development practice, I have applied several design patterns that help make the code more maintainable and extensible:
- Singleton — for creating a single instance of a class, for example, for a logger or configuration manager.
- Factory Method — for creating objects without tight coupling to specific classes, facilitating functionality extension.
- Repository — for abstracting data access, simplifying testing and changing data sources.
- Dependency Injection — for managing dependencies between classes, improving modularity and testability.
Example of using Singleton in C#:
public class Logger
{
private static Logger _instance;
private static readonly object _lock = new object();
private Logger() { }
public static Logger Instance
{
get
{
lock (_lock)
{
if (_instance == null)
_instance = new Logger();
return _instance;
}
}
}
public void Log(string message)
{
Console.WriteLine(message);
}
}
Using such patterns helps create flexible and easily maintainable applications.