Sobes.tech
Senior

Tell me about design patterns: groups and examples.

sobes.tech AI

Answer from AI

Design patterns are proven solutions to common software development problems. They can be divided into three main categories:

  1. Creational — responsible for object creation, hiding complexity and providing flexibility:

    • Singleton — ensures a class has only one instance.
    • Factory Method — defines an interface for creating an object, allowing subclasses to alter the type of objects that will be created.
    • Abstract Factory — provides an interface for creating families of related or dependent objects.
    • Builder — separates the construction of a complex object from its representation.
    • Prototype — creates new objects by copying existing ones.
  2. Structural — define ways to compose classes and objects:

    • Adapter — allows objects with incompatible interfaces to work together.
    • Decorator — dynamically adds responsibilities to objects.
    • Facade — provides a simplified interface to a complex subsystem.
    • Composite — allows grouping objects into tree structures.
    • Proxy — provides a surrogate or placeholder for another object to control access.
  3. Behavioral — responsible for interaction between objects:

    • Observer — notifies subscribers about changes in an object's state.
    • Strategy — defines a family of algorithms, encapsulates each one, and makes them interchangeable.
    • Command — encapsulates a request as an object, allowing parameterization of clients.
    • Iterator — provides a way to access elements of a collection sequentially.
    • State — allows an object to alter its behavior when its internal state changes.

Understanding and applying patterns helps create flexible, extendable, and maintainable architectures.