Lecture 11 - Decorator Design Pattern
Lecture 11 - Decorator Design Pattern
@Override
public String decorate() {
return "Christmas tree";
}
}
Example of Decorator Design Pattern
We’ll now create an abstract TreeDecorator class for this tree. This decorator will implement the ChristmasTree
interface as well as hold the same object. The implemented method from the same interface will simply call the
decorate() method from our interface:
// standard constructors
@Override
public String decorate() {
return tree.decorate();
}
}
Example of Decorator Design Pattern
We’ll now create some decorating element. These decorators will extend our abstract TreeDecorator class and will modify its
decorate() method according to our requirement:
Note that in the first tree1 object, we’re only decorating it with only one Garland, while the other tree2 object we’re
decorating with one BubbleLights and two Garlands. This pattern gives us this flexibility to add as many decorators as we
want at runtime.
References
1. https://baeldung.com/java-decorator-pattern
2. https://www.geeksforgeeks.org/decorator-design-pattern-in-java-with-example/
3. https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
4. https://www.javatpoint.com/decorator-pattern
Thank You