Composite Header
-
namespace composite_pattern
Namespace for the Composite pattern.
-
class Component
- #include <composite.hpp>
The Component interface declares common operations for both simple and complex objects.
Subclassed by composite_pattern::Composite, composite_pattern::Leaf
Public Functions
-
virtual ~Component() = default
-
virtual void operation() const = 0
Performs the operation for the component.
-
inline virtual void add(std::shared_ptr<Component>)
Adds a child component (only meaningful for Composite).
-
inline virtual void remove(std::shared_ptr<Component>)
Removes a child component (only meaningful for Composite).
-
inline virtual bool isComposite() const
Checks if the component is a composite.
- Returns:
True if composite, otherwise false.
-
virtual ~Component() = default
-
class Composite : public composite_pattern::Component
- #include <composite.hpp>
The Composite class represents the complex components that may have children.
A Composite object can have both Leaf and other Composite objects as children.
Public Functions
-
inline virtual void add(std::shared_ptr<Component> component) override
Adds a child component to the composite.
- Parameters:
component – The child component to add.
-
inline virtual void remove(std::shared_ptr<Component> component) override
Removes a child component from the composite.
- Parameters:
component – The child component to remove.
-
inline virtual bool isComposite() const override
Checks if the component is a composite.
- Returns:
True, as this is a Composite.
-
inline virtual void operation() const override
Performs the operation for the composite and delegates to child components.
Private Members
-
std::vector<std::shared_ptr<Component>> children
List of child components.
-
inline virtual void add(std::shared_ptr<Component> component) override
-
class Leaf : public composite_pattern::Component
- #include <composite.hpp>
The Leaf class represents the end objects of a composition.
A Leaf cannot have any children. It implements the base Component interface.
Public Functions
-
inline explicit Leaf(const std::string &name)
Constructs a Leaf with a given name.
- Parameters:
name – The name of the leaf.
-
inline virtual void operation() const override
Performs the operation specific to the Leaf.
Private Members
-
std::string name
The name of the leaf for identification.
-
inline explicit Leaf(const std::string &name)
-
class Component