Composite API Documentation

class Component[source]

Bases: ABC

The Component interface declares common operations for both leaf and composite objects.

abstractmethod operation()[source]

Perform an operation on the component.

Return type:

None

add(component)[source]

Optionally, add a child component to the composite. Raises NotImplementedError for leaf components.

Parameters:

component (Component) – The child component to add.

Return type:

None

remove(component)[source]

Optionally, remove a child component from the composite. Raises NotImplementedError for leaf components.

Parameters:

component (Component) – The child component to remove.

Return type:

None

get_child(index)[source]

Optionally, retrieve a child component at a specific index. Raises NotImplementedError for leaf components.

Parameters:

index (int) – The index of the child component.

Return type:

Component

Returns:

The child component.

class Leaf(name)[source]

Bases: Component

Represents the Leaf object in the Composite pattern. A Leaf has no children and defines behavior for primitive objects.

Parameters:

name (str)

__init__(name)[source]

Initialize the Leaf with a name.

Parameters:

name (str) – The name of the leaf.

operation()[source]

Perform the operation specific to the leaf.

Return type:

None

class Composite(name)[source]

Bases: Component

Represents the Composite object in the Composite pattern. A Composite can contain both Leaf and other Composite objects.

Parameters:

name (str)

__init__(name)[source]

Initialize the Composite with a name.

Parameters:

name (str) – The name of the composite.

operation()[source]

Perform the operation on the composite and its children.

Return type:

None

add(component)[source]

Add a child component to the composite.

Parameters:

component (Component) – The child component to add.

Return type:

None

remove(component)[source]

Remove a child component from the composite.

Parameters:

component (Component) – The child component to remove.

Return type:

None

get_child(index)[source]

Retrieve a child component at a specific index.

Parameters:

index (int) – The index of the child component.

Return type:

Component

Returns:

The child component.