Decorative (Decorator) design pattern
- Dynamically adding some extra functionality to an object, the decorative pattern is more flexible than generating subclasses for added functionality
- The ability to dynamically extend an object without changing the original class file and using inheritance (this is the benefit of the decorative design pattern)
- The decorative design pattern is to create a wrapper object that uses decorations to wrap real objects.
Application Scenarios:
- Need to extend the functionality of a class, or add additional responsibilities to a class
- When it is not possible to implement by generating subclasses, for example, the final class
How to implement the decoration mode:
- Decorative objects and real objects have the same interface
- A decorative object contains a reference to a real object
- All methods of decorating objects, internal implementations are invoked through references to real objects, and then implement their own functions,
Java Design Patterns