Structural design patterns deal with object composition, and aim to create relationships between objects to compose more complex structures. Here is a brief overview of each of the structural design patterns:
- Adapter pattern: This pattern allows for the integration of incompatible interfaces. It consists of a class that converts the interface of one class into the interface expected by another class. For example, an adapter class for integrating a legacy system with a modern application might have methods that map the legacy system’s functions to methods that the modern application can understand.
- Bridge pattern: This pattern separates an abstraction from its implementation, allowing the two to vary independently. It consists of an abstract class that defines the interface for the abstraction, and a separate concrete class that implements the interface. For example, a bridge class for connecting a mobile app to various social media platforms might have an abstract “post” method that is implemented differently for each platform.
- Composite pattern: This pattern allows for the creation of hierarchical tree structures. It consists of a class that represents both individual objects and compositions of objects, allowing for the creation of recursive tree structures. For example, a composite class for creating a file system might have methods for adding and removing files and directories, and the directories themselves would be instances of the composite class.
- Decorator pattern: This pattern allows for the addition of new behavior to existing objects without modifying their code. It consists of a class that wraps an object and adds new behavior to it. For example, a decorator class for logging the calls made to an object might have methods that intercept calls to the wrapped object and log them before forwarding the calls on.
- Facade pattern: This pattern provides a simplified interface for a complex system. It consists of a class that exposes a simple interface to the client, hiding the complexity of the underlying system. For example, a facade class for interacting with a database might have methods for common tasks such as inserting and querying data, hiding the complexity of the SQL queries and connections from the client.
- Flyweight pattern: This pattern allows for the efficient use of memory by sharing common data among objects. It consists of a class that stores the shared data and a factory class that returns instances of the class with the shared data. For example, a flyweight class for storing strings might store only the unique characters in each string, and the factory class would return instances of the flyweight class with the appropriate shared data