9.5.2 Structural (Adapter, Decorator)
Appearance
9.5.2 Structural (Adapter, Decorator)
Structural design patterns are about how objects and classes are put together to form larger structures. They help make these structures flexible and efficient.
- Adapter: Imagine you have a European power plug, but you're in the United States where the outlets are different. You'd use a power adapter to make them compatible. The Adapter pattern allows two incompatible interfaces (ways of doing things) to work together. It acts as a bridge between them, translating requests so they can understand each other. This is useful when you want to use an existing class that doesn't quite fit the way your new code expects things to work.
- Decorator: Think about ordering an ice cream sundae. You start with plain ice cream, but then you can add sprinkles, whipped cream, a cherry, and chocolate sauce. Each addition 'decorates' the sundae, adding new features without changing the original ice cream. The Decorator pattern allows you to add new behaviors or responsibilities to an object dynamically (while the program is running) without changing its original code. It's a flexible way to extend an object's functionality.
Bibliography:
- Gamma, Erich, Helm, Richard, Johnson, Ralph, and Vlissides, John. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional, 1994.