Complete the code to declare the base component interface.
interface [1] {
void operation();
}The base interface is named Component which defines the operation method to be decorated.
Complete the code to declare the decorator class that implements the component interface.
class [1] implements Component { protected Component component; public [1](Component component) { this.component = component; } public void operation() { component.operation(); } }
The decorator class is named Decorator and it implements the Component interface, forwarding calls to the wrapped component.
Fix the error in the concrete decorator's operation method to add extra behavior.
class ConcreteDecorator extends Decorator { public ConcreteDecorator(Component component) { super(component); } public void operation() { [1]; super.operation(); } }
component.operation() directly instead of using super.operation().operation() recursively causing infinite loop.The concrete decorator adds extra behavior by calling extraBehavior() before delegating to the base operation.
Fill both blanks to implement a decorator that adds logging before and after the operation.
class LoggingDecorator extends Decorator { public LoggingDecorator(Component component) { super(component); } public void operation() { System.out.println([1]); super.operation(); System.out.println([2]); } }
The logging decorator prints "Start operation" before and "End operation" after the base operation.
Fill all three blanks to create a client code that wraps a concrete component with two decorators.
Component component = new ConcreteComponent(); Component decorated = new [1](component); Component doubleDecorated = new [2](decorated); doubleDecorated.[3]();
The client wraps the ConcreteComponent first with ConcreteDecorator, then with LoggingDecorator, and calls operation() on the outermost decorator.