0
0
LLDsystem_design~10 mins

Decorator pattern in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the base component interface.

LLD
interface [1] {
    void operation();
}
Drag options to blanks, or click blank then click option'
AComponent
BClient
CConcreteComponent
DDecorator
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Decorator' as the base interface name instead of 'Component'.
Naming the interface after the concrete implementation.
2fill in blank
medium

Complete the code to declare the decorator class that implements the component interface.

LLD
class [1] implements Component {
    protected Component component;
    public [1](Component component) {
        this.component = component;
    }
    public void operation() {
        component.operation();
    }
}
Drag options to blanks, or click blank then click option'
ADecorator
BConcreteDecorator
CConcreteComponent
DClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ConcreteDecorator' as the decorator class name here.
Not implementing the component interface.
3fill in blank
hard

Fix the error in the concrete decorator's operation method to add extra behavior.

LLD
class ConcreteDecorator extends Decorator {
    public ConcreteDecorator(Component component) {
        super(component);
    }
    public void operation() {
        [1];
        super.operation();
    }
}
Drag options to blanks, or click blank then click option'
Asuper.operation()
Bcomponent.operation()
CextraBehavior()
Doperation()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling component.operation() directly instead of using super.operation().
Calling operation() recursively causing infinite loop.
4fill in blank
hard

Fill both blanks to implement a decorator that adds logging before and after the operation.

LLD
class LoggingDecorator extends Decorator {
    public LoggingDecorator(Component component) {
        super(component);
    }
    public void operation() {
        System.out.println([1]);
        super.operation();
        System.out.println([2]);
    }
}
Drag options to blanks, or click blank then click option'
A"Start operation"
B"End operation"
C"Operation started"
D"Operation finished"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the start and end messages.
Using the same message for both prints.
5fill in blank
hard

Fill all three blanks to create a client code that wraps a concrete component with two decorators.

LLD
Component component = new ConcreteComponent();
Component decorated = new [1](component);
Component doubleDecorated = new [2](decorated);
doubleDecorated.[3]();
Drag options to blanks, or click blank then click option'
ALoggingDecorator
BConcreteDecorator
Coperation
DDecorator
Attempts:
3 left
💡 Hint
Common Mistakes
Using the base Decorator class directly instead of concrete decorators.
Calling a method other than operation.