12 Essential Structural Design Patterns
In the realm of software development, structural design patterns provide proven solutions to recurring problems. They offer tested, modular solutions to design issues, making your code more flexible, maintainable, and understandable. Here, we explore a comprehensive list of structural design patterns, categorizing them based on their fundamental behaviors, and delving into examples and implementations.
Structural design patterns focus on how to compose objects into larger structures while keeping these objects independent of each other and the structures that contain them. This article aims to provide a solid foundation for understanding and applying these patterns in your software design.
Decorator Pattern
The decorator pattern attaches additional responsibilities to an object dynamically. It provides a flexible alternative to subclassing for extended functionality.
Here's an example in Python, where a parenting responsibility is added to an employee object:
Employee and Parent
First, let's define our basic employee and parent roles:
- Employee: A basic employee with a job title.
- Parent: A role that extends the employee with additional responsibilities, like attending parent-teacher conferences.
Now, we can create these classes:
class Employee: def __init__(self, title): self.title = title
def work(self): print(f"{self.title} is working.")
class Parent(Employee): def __init__(self, title): super().__init__(title) def take_to_school(self): print(f"{self.title} is taking the child to school.")
Applying the Decorator Pattern
To apply the decorator pattern, we'll create a Decorator class that wraps an existing employee and adds new behaviors. Here's how:
First, we need a new interface for our decorators:
class EmployeeDecorator: def work(self): pass
Then, we can implement our parent decorator:
Again, note the use of the super().__init__(employee) line, which initializes the decorator with the wrapped employee. This ensures that the decorator has access to the methods and properties of the employee it decorates.
Adapter Pattern
The adapter pattern allows incompatible interfaces to work together. It wraps an interface to provide a different interface that a client expects.
Here's an example in Java, where we have a class that requires a specific interface, but we only have an implementation that matches a different interface:
Adapter Interface and OldClass
First, let's define our required interface and the old class that we want to use:
- RequiredInterface: The interface required by the class that calls our functionality.
- OldClass: The class we want to use, but it doesn't implement
RequiredInterface.
Next, we'll implement the adapter class:
Finally, in our main class, we can use the adapter to bridge the gap between the old class and the required interface:
RequiredInterface adapter = new OldClassAdapter(new OldClass());
adapter.requiredMethod();
A real-world example of this pattern would be integrating legacy software with a modern API. The legacy software might have a different method naming convention or require different inputs than the modern API expects. In this case, an adapter would translate between the two.