Bird
0
0
LLDsystem_design~10 mins

Visitor 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 Visitor interface method for visiting an Element.

LLD
interface Visitor {
    void visit([1] element);
}
Drag options to blanks, or click blank then click option'
AElement
BVisitor
CObject
DElementVisitor
Attempts:
3 left
💡 Hint
Common Mistakes
Using Visitor as parameter type instead of Element.
Using Object which is too generic.
2fill in blank
medium

Complete the code to implement the accept method in the Element class that accepts a Visitor.

LLD
class Element {
    void accept([1] visitor) {
        visitor.visit(this);
    }
}
Drag options to blanks, or click blank then click option'
AObject
BElement
CVisitor
DElementVisitor
Attempts:
3 left
💡 Hint
Common Mistakes
Using Element as parameter type instead of Visitor.
Using Object which is too generic.
3fill in blank
hard

Fix the error in the ConcreteVisitor class method signature for visiting an Element.

LLD
class ConcreteVisitor implements Visitor {
    public void visit([1] element) {
        // operation implementation
    }
}
Drag options to blanks, or click blank then click option'
AConcreteElement
BElement
CObject
DVisitor
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConcreteElement which is more specific than the interface.
Using Visitor or Object which do not match the interface.
4fill in blank
hard

Fill both blanks to complete the double dispatch call in the client code.

LLD
Element element = new ConcreteElement();
Visitor visitor = new ConcreteVisitor();
element.[1](visitor);
visitor.[2](element);
Drag options to blanks, or click blank then click option'
Aaccept
Bvisit
Cexecute
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using execute or handle which are not standard Visitor pattern methods.
Swapping accept and visit method calls.
5fill in blank
hard

Fill all three blanks to complete the Visitor pattern structure with multiple elements and visitors.

LLD
interface Visitor {
    void visit([1] elementA);
    void visit([2] elementB);
}

class ConcreteVisitor implements Visitor {
    public void visit([3] elementA) {
        // implementation
    }
    public void visit(ElementB elementB) {
        // implementation
    }
}
Drag options to blanks, or click blank then click option'
AElementA
BElementB
DElementC
Attempts:
3 left
💡 Hint
Common Mistakes
Using ElementC which is not declared in the interface.
Mismatching parameter types between interface and implementation.