Junior — Middle
Can you list and explain the main design patterns you have encountered?
sobes.tech AI
Answer from AI
Common design patterns encountered in C#:
- Singleton — ensures that a class has only one instance and provides a global point of access to it.
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private Singleton() { }
public static Singleton Instance => instance;
}
-
Factory Method — defines an interface for creating an object but allows subclasses to alter the type of objects that will be created.
-
Observer — allows objects to subscribe to events of another object and receive notifications about changes.
-
Decorator — dynamically adds responsibilities to objects by wrapping them in other objects.
-
Strategy — defines a family of algorithms, encapsulates each one, and makes them interchangeable.
These patterns help in writing flexible, extendable, and maintainable code.