Complete the code to declare the base component interface in the Composite pattern.
interface Component {
void [1]();
}The base component interface declares the common operation method that both leaf and composite classes implement.
Complete the code to add a child component in the Composite class.
class Composite implements Component { private List<Component> children = new ArrayList<>(); public void [1](Component component) { children.add(component); } public void operation() { for (Component child : children) { child.operation(); } } }
The Composite class manages child components by adding them with the addChild method.
Fix the error in the Leaf class operation method to correctly implement the Composite pattern.
class Leaf implements Component { public void [1]() { System.out.println("Leaf operation"); } }
The Leaf class implements the operation method from the Component interface to perform its specific action.
Fill both blanks to define the Composite class constructor and initialize the children list.
class Composite implements Component { private List<Component> children; public Composite() { children = new [1]<>(); } public void addChild(Component component) { children.[2](component); } }
The Composite class initializes its children list with ArrayList and adds components using the add method.
Fill all three blanks to implement a recursive operation method in Composite that calls operation on all children.
class Composite implements Component { private List<Component> children = new ArrayList<>(); public void operation() { for ([1] : children) { [2].[3](); } } }
The for-each loop iterates over each child component, calling its operation method recursively.