Sobes.tech
Junior — Middle

Can you tell about different structural patterns in object-oriented design?

sobes.tech AI

Answer from AI

Structural design patterns help organize classes and objects for ease of use and extension, providing a flexible structure.

Main structural patterns:

  • Adapter — allows objects with incompatible interfaces to work together by wrapping one interface in another.

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

  • Facade — provides a simplified interface to a complex subsystem.

  • Proxy — controls access to another object, for example, for lazy initialization or access control.

  • Composite — allows grouping objects into a tree structure and working with them uniformly.

  • Bridge — separates abstraction and implementation, allowing them to vary independently.

Example of using Decorator in C#:

interface IComponent {
    void Operation();
}

class ConcreteComponent : IComponent {
    public void Operation() {
        Console.WriteLine("Main operation");
    }
}

class Decorator : IComponent {
    protected IComponent component;
    public Decorator(IComponent component) {
        this.component = component;
    }
    public virtual void Operation() {
        component.Operation();
    }
}

class ConcreteDecorator : Decorator {
    public ConcreteDecorator(IComponent component) : base(component) {}
    public override void Operation() {
        base.Operation();
        Console.WriteLine("Additional behavior");
    }
}

These patterns help create flexible and maintainable architectures.

Can you tell about different structural patterns in… - sobes.tech