mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 10:36:39 +00:00
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from dataclasses import dataclass, field
|
|
from typing import List
|
|
|
|
from structural.composite.component import Component
|
|
|
|
|
|
@dataclass
|
|
class Composite(Component):
|
|
"""
|
|
The Composite class represents the complex components that may have
|
|
children. Usually, the Composite objects delegate the actual work to their
|
|
children and then "sum-up" the result.
|
|
"""
|
|
|
|
_children: List[Component] = field(default_factory=list)
|
|
|
|
"""
|
|
A composite object can add or remove other components (both simple or
|
|
complex) to or from its child list.
|
|
"""
|
|
|
|
def add(self, component: Component) -> None:
|
|
self._children.append(component)
|
|
component.parent = self
|
|
|
|
def remove(self, component: Component) -> None:
|
|
self._children.remove(component)
|
|
component.parent = None
|
|
|
|
def is_composite(self) -> bool:
|
|
return True
|
|
|
|
def operation(self) -> str:
|
|
"""
|
|
The Composite executes its primary logic in a particular way. It
|
|
traverses recursively through all its children, collecting and summing
|
|
their results. Since the composite's children pass these calls to their
|
|
children and so forth, the whole object tree is traversed as a result.
|
|
"""
|
|
|
|
results = []
|
|
for child in self._children:
|
|
results.append(child.operation())
|
|
return f"Branch({'+'.join(results)})"
|