Structural patterns

1 min read

Structural patterns concern class and object composition, forming larger structures from individual parts.

Decorator

Intent: attach additional responsibilities or behaviors to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Use cases: adding features like logging, caching or compression to an existing object without modifying its class. C# streams are a good example, such as a CryptoStream wrapping a FileStream.

Adapter

Intent: convert the interface of a class into another interface that clients expect. Adapter lets classes work together that couldn’t otherwise, because of incompatible interfaces.

Use cases: integrating with legacy systems or third-party libraries whose interfaces differ from what your system expects.

Facade

Intent: provide a simplified, unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Use cases: simplifying interaction with a complex library or framework by giving it a single, straightforward entry point.

Proxy

Intent: provide a surrogate or placeholder for another object, to control access to it.

Use cases: lazy initialization (virtual proxy), access control (protection proxy), logging requests (logging proxy), or facilitating remote communication (remote proxy). EF Core uses proxies for lazy loadingLoading related dataEager, explicit and lazy loading, and the N+1 problem that lazy loading can cause.Opens in a new tab.

The other two categories are creationalCreational patternsFour patterns for creating objects in a way that suits the situation.Opens in a new tab and behavioralBehavioral patternsFour patterns for how objects interact and share out responsibilities.Opens in a new tab.


Sources