Compose objects into tree structures to represent part-whole hierarchies, letting clients treat individual objects and compositions of objects uniformly. Use it when you want to represent part-whole hierarchies and when you want clients to be able to ignore the difference between compositions of objects and individual objects.
Examples:
- An army consists of divisions, each containing brigades, which contain platoons, which contain squads of individual soldiers. An order ('march!') is issued at the top and passed down each level — the same command works on a single soldier (leaf) or an entire army (composite) through a shared execute() interface.
// Component interface shared by Leaf and Composite
class Graphic {
public:
virtual void Draw(const Point& at) = 0;
virtual void Add(Graphic*) {}
virtual void Remove(Graphic*) {}
virtual List<Graphic*>::Iterator* CreateIterator() { return 0; }
};
class Picture : public Graphic {
public:
void Draw(const Point& at) override;
void Add(Graphic* g) override { _graphics.Append(g); }
private:
List<Graphic*> _graphics;
};
Synonyms: object tree