Structural patterns are design patterns that deal with object composition to form larger structures, such as class or object hierarchies. These patterns help to make the system more organized, modular, and extensible.
Below is an example of a popular structural pattern called the Adapter pattern:
Adapter Pattern: It is used to convert the interface of a class into another interface that the client expects. This pattern involves two types of interfaces, i.e., the adapter class, which adapts the interface of the adaptee class, to that of the target interface.
Example: Consider a scenario where we want to connect a USB device to a laptop which has only a PS/2 port. In this case, we can use an adapter to convert the interface of the USB to PS/2 port. The Adapter Pattern can be used here as a mediator between the USB adapter and the Laptop port – the adapter class adapts the USB interface to the PS/2 interface, and hence, the laptop can now connect to the USB device via the adapter.
Structural patterns provide solutions for organizing objects and classes into larger structures while maintaining flexibility and simplicity.
These patterns deal with the composition of objects and how they can be arranged to form complex structures, such as trees or graphs.
Structural patterns include three main categories: class patterns, object patterns, and composition patterns.
Class patterns focus on creating or altering class relationships to achieve desired behaviors, such as inheritance or proxy patterns.
Object patterns involve creating relationships between objects to simplify interactions and improve performance, such as adapter or decorator patterns.
Composition patterns involve constructing complex objects from simpler objects or promoting the independence of objects from their environment, such as composite or flyweight patterns.
Structural patterns can improve the flexibility and maintainability of code by separating concerns and facilitating communication between objects.
However, the use of structural patterns can also introduce additional complexity and reduce performance, so they should be used judiciously and appropriately.
What is the Structural Pattern that provides an interface for creating objects, but lets subclasses decide which class to instantiate?
Answer: The Factory method pattern.
Which Structural Pattern is used to create a one-to-many relationship between objects, where one object maintains a list of dependent objects and notifies them automatically of any changes to its state?
Answer: The Observer pattern.
Which Structural Pattern is used to provide a simplified interface to a complex system of classes, making it easy to use and understand for clients?
Answer: The Facade pattern.
What is the Structural Pattern that allows you to add new functionality to an object without changing its underlying structure, by wrapping it in a new object that provides the extra behavior?
Answer: The Decorator pattern.
Which Structural Pattern is used to define a family of related classes with a common interface, allowing these classes to be interchanged without affecting the client code that uses them?
Answer: The Abstract Factory pattern.