It lets you treat individual objects and groups of objects (composites) in a similar way.
A structural pattern that lets you treat individual objects and groups of objects uniformly through a common interface. Represent part-whole hierarchies so that clients can treat leaf and composite nodes the same — enabling recursive tree structures.
- Component (interface/abstract class): Common interface for both leaf and composite.
- Leaf: A basic object that has no children.
- Composite: A container that holds other components (can be leaf or composite
class IFileIntf {
public:
virtual ~IFileIntf() = default;
virtual void print() const = 0;
};
class File : public IFileIntf {
public:
File (string fName) : name(fName) {
}
void print() const {
cout <<" file name " << name <<endl;
}
private:
string name;
};
class FileDirectory : public IFileIntf {
public:
FileDirectory (const string &fName) : name(fName) {
}
void add(unique_ptr<IFileIntf> f) {
files.push_back(std::move(f));
}
void print() const;
private:
std::string name;
std::vector<unique_ptr<IFileIntf>> files;
};
void FileDirectory::print() const {
cout <<" name :: " << name << endl;
for (auto & f : files) {
f->print();
}
}
int main() {
auto fd = make_unique<FileDirectory>("root");
fd->add(make_unique<File>("f1"));
fd->add(make_unique<File>("f2"));
unique_ptr<FileDirectory> fdinner = make_unique<FileDirectory>("fdinner");
fdinner->add(make_unique<File>("f3"));
fd->add(std::move(fdinner));
fd->print();
}
Another example :
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
// Component
class View {
public:
virtual void draw() = 0;
virtual ~View() = default;
};
// Leaf
class TextView : public View {
public:
void draw() override {
cout << "Drawing TextView\n";
}
};
// Composite
class ViewGroup : public View {
vector<shared_ptr<View>> children;
public:
void add(shared_ptr<View> view) {
children.push_back(view);
}
void draw() override {
cout << "Drawing ViewGroup\n";
for (auto& child : children) {
child->draw();
}
}
};
// Usage
int main() {
auto root = make_shared<ViewGroup>();
auto text1 = make_shared<TextView>();
auto innerGroup = make_shared<ViewGroup>();
innerGroup->add(make_shared<TextView>());
root->add(text1);
root->add(innerGroup);
root->draw(); // recursive composite draw
return 0;
}
Follow more posts @ https://jdecodes.wordpress.com
My all design pattern articles :
- https://jdecodes.wordpress.com/2024/07/13/builder-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/command-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/iterator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/mediator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/state-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/memento-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/observer-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/strategy-pattern/
- https://jdecodes.wordpress.com/2024/07/20/visitor-design-pattern/
- https://jdecodes.wordpress.com/2024/07/20/adapter-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/20/bridge-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/composite-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/22/facade-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/decorater-design-pattern/
Leave a comment