Sobes.tech
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#:

  1. 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;
}
  1. Factory Method — defines an interface for creating an object but allows subclasses to alter the type of objects that will be created.

  2. Observer — allows objects to subscribe to events of another object and receive notifications about changes.

  3. Decorator — dynamically adds responsibilities to objects by wrapping them in other objects.

  4. Strategy — defines a family of algorithms, encapsulates each one, and makes them interchangeable.

These patterns help in writing flexible, extendable, and maintainable code.