Structural Design Patterns

What are Structural Design Patterns ?

Structural design patterns are a group of design patterns that focus on class and object composition to form larger structures. They help define how classes and objects are connected or related to provide new functionalities. The primary goal of structural design patterns is to ensure that classes and objects work together effectively and efficiently to achieve system scalability and flexibility.

Most Used Structural design patterns:

Adapter Pattern:


The Adapter pattern allows incompatible interfaces to work together by providing a wrapper (adapter) that converts one interface into another. It acts as a bridge between two interfaces, allowing them to collaborate without modifying their original code.

Example:
Adapting an old legacy class with a non-compliant interface to work with a new system that requires a different interface.

Bridge Pattern:


The Bridge pattern separates an object’s abstraction from its implementation, allowing them to vary independently. It decouples abstraction from its implementation, enabling both to be modified independently without affecting each other.

Example:
Creating different types of shapes with different drawing implementations, where the shapes can have different colors and sizes.

Composite Pattern:


The Composite pattern composes objects into tree-like structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions uniformly, simplifying the handling of complex structures.

Example:
Creating a tree-like structure for a file system where a folder can contain files and sub-folders, and the same operations can be performed on both files and folders.

Decorator Pattern:


The Decorator pattern dynamically adds new behavior to objects without modifying their code. It provides a flexible alternative to subclassing for extending functionality.

Example:
Extending the functionalities of a text editor by adding new features (e.g., spell-checking, text formatting) through decorator classes without changing the original text editor class.

Facade Pattern:


The Facade pattern provides a unified interface to a set of interfaces in a subsystem, simplifying complex systems and reducing dependencies. It acts as a higher-level interface that makes it easier to use the subsystem.

Example:
Creating a high-level API that simplifies the interactions with a complex library, abstracting away the underlying complexities.

Flyweight Pattern:


The Flyweight pattern shares objects to reduce memory usage when many similar objects are needed. It aims to minimize memory consumption by reusing shared objects instead of creating new ones.

Example:
Managing graphical objects in a computer game, where multiple instances of the same graphic (e.g., trees, rocks) can be shared to conserve memory.

Proxy Pattern:


The Proxy pattern provides a surrogate or placeholder for another object to control access to it. It acts as an intermediary or a protective barrier to the real object, adding extra functionalities or security checks.

Example:
Using a proxy to control access to sensitive resources (e.g., a network connection) and perform authentication before allowing access.

Each of these structural design patterns addresses specific collaboration and composition challenges and promotes the flexibility and maintainability of software systems. By applying these patterns, developers can create a more modular and adaptable codebase that can evolve and scale with changing requirements.