Bird
0
0
LLDsystem_design~10 mins

Template Method 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 define the abstract method in the base class.

LLD
class AbstractClass:
    def template_method(self):
        self.base_operation1()
        self.[1]()
        self.base_operation2()

    def base_operation1(self):
        print("Base operation 1")

    def base_operation2(self):
        print("Base operation 2")

    def [1](self):
        pass
Drag options to blanks, or click blank then click option'
Aprimitive_operation
Btemplate_method
Cbase_operation1
Dhook
Attempts:
3 left
💡 Hint
Common Mistakes
Using the template_method name for the abstract method.
Overriding base_operation1 instead of the abstract method.
2fill in blank
medium

Complete the code to override the primitive operation in the subclass.

LLD
class ConcreteClass(AbstractClass):
    def [1](self):
        print("Concrete implementation of primitive operation")
Drag options to blanks, or click blank then click option'
Atemplate_method
Bbase_operation2
Cprimitive_operation
Dhook
Attempts:
3 left
💡 Hint
Common Mistakes
Overriding template_method instead of primitive_operation.
Not overriding any method.
3fill in blank
hard

Fix the error in the template method to call the correct primitive operation.

LLD
def template_method(self):
    self.base_operation1()
    self.[1]()
    self.base_operation2()
Drag options to blanks, or click blank then click option'
Aprimitive_operation
Btemplate_method
Cbase_operation1
Dhook
Attempts:
3 left
💡 Hint
Common Mistakes
Calling template_method recursively causing infinite loop.
Calling base_operation1 twice.
4fill in blank
hard

Fill both blanks to add a hook method and call it in the template method.

LLD
class AbstractClass:
    def template_method(self):
        self.base_operation1()
        self.primitive_operation()
        self.[1]()
        self.base_operation2()

    def [2](self):
        pass
Drag options to blanks, or click blank then click option'
Ahook
Bprimitive_operation
Dtemplate_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both blanks incorrectly.
Not defining the hook method at all.
5fill in blank
hard

Fill all three blanks to implement a concrete hook method and call the template method.

LLD
class ConcreteClass(AbstractClass):
    def primitive_operation(self):
        print("Concrete primitive operation")

    def [1](self):
        print("Concrete hook operation")

obj = ConcreteClass()
obj.[2]()

# Output should show base_operation1, primitive_operation, hook, base_operation2

# Call the template method named [3]
Drag options to blanks, or click blank then click option'
Ahook
Btemplate_method
Dprimitive_operation
Attempts:
3 left
💡 Hint
Common Mistakes
Calling primitive_operation directly instead of template_method.
Not overriding the hook method.