Head First Design pattern reading notes (3) Decorator pattern decorator mode Decorator pattern class diagram
Definition
Decorator mode: By allowing the component class to implement the same interface as the decorator class, the adornment class can dynamically expand the new functionality of the component class without modifying the original component class, and can expand indefinitely.
Some of the original test of OO
- Class should be closed for modification, open for expansion. –> "Open and close principle", that is, try not to modify the already used classes, and through the way of inheritance to expand the new functions of the class.
- When designing a class, you should consider not modifying the original code as much as possible. –> "Open and closed principle"
- The way you combine and broker is often used to dynamically add new features at run time.
about decorator mode
- Decorator mode each new feature will reseal a decorator class, which can lead to the "class explosion" problem, the code will have a lot of similar structure of the class, for the first contact with the person is not good understanding and maintenance.
- The Decorator class is a mirror of the component class it decorates, with all the functionality of the original component and new functionality.
- If the adorner class has the same function signature method as the component class, in this method, the decorator adds new processing logic before/after calling the component method to add new functionality to the component.
- The input and output stream related classes in Java are implemented through decorator pattern.
Head First Design pattern reading notes (3) Decorator mode