Composite pattern
In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.
In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly. ==Overview== The Composite design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. ===Problems=== The Composite pattern solves these problems: Represent a part-whole hierarchy so that clients can treat part and whole objects uniformly. Represent a part-whole hierarchy as tree structure. When defining (1) Part objects and (2) Whole objects that act as containers for Part objects, clients must treat them separately, which complicates client code. ===Solution=== Define a unified Component interface for part ( Leaf ) objects and whole ( Composite ) objects. Individual Leaf objects implement the Component interface directly, and Composite objects forward requests to their child components. This enables clients to work through the Component interface to treat Leaf and Composite objects uniformly: Leaf objects perform a request directly, and Composite objects forward the request to their child components recursively downwards the tree structure. This makes client classes easier to implement, change, test, and reuse. See also the UML class and object diagram below. ==Structure== import std; using std::runtime_error; using std::shared_ptr; using std::string; using std::unique_ptr; using std::vector; // Component object // declares the interface for objects in the composition. class Equipment { private: string name; double netPrice; protected: Equipment() = default; explicit Equipment(const string& name): name{name}, netPrice{0} {} public: // implements default behavior for the interface common to all classes, as appropriate. nodiscard virtual const string& getName() const noexcept { return name; } virtual void setName(const string& name) noexcept { this->name = name; } nodiscard virtual double getNetPrice() const noexcept { return netPrice; } virtual void setNetPrice(double netPrice) noexcept { this->netPrice = netPrice; } // declares an interface for accessing and managing its child components. virtual void add(shared_ptr ) = 0; virtual void remove(shared_ptr ) = 0; virtual ~Equipment() = default; }; // Composite object // defines behavior for components having children. class CompositeEquipment: public Equipment { private: // stores child components. using EquipmentList = vector >; EquipmentList equipments; protected: CompositeEquipment() = default; explicit CompositeEquipment(const string& name): Equipment(name), equipments{EquipmentList()} {} public: // implements child-related operations in the Component interface. nodiscard virtual double getNetPrice() const noexcept override { double total = Equipment::getNetPrice(); for (const Equipment& i: equipments) { total += i->getNetPrice(); } return total; } virtual void add(shared_ptr equipment) override { equipments.push_back(equipment.get()); } virtual void remove(shared_ptr equipment) override { equipments.remove(equipment.get()); } }; // Leaf object // represents leaf objects in the composition. class FloppyDisk: public Equipment { public: explicit FloppyDisk(const String& name): Equipment(name) {} // A leaf has no children. void add(shared_ptr ) override { throw runtime_error("FloppyDisk::add() cannot be called!"); } void remove(shared_ptr ) override { throw runtime_error("FloppyDisk::remove() cannot be called!"); } }; class Chassis: public CompositeEquipment { public: explicit Chassis(const string& name): CompositeEquipment(name) {} }; int main() { shared_ptr fd1 = std::make_shared ("3.5in Floppy"); fd1->setNetPrice(19.99); std::println("{}: netPrice = {}", fd1->getName(), fd1->getNetPrice); shared_ptr fd2 = std::make_shared ("5.25in Floppy"); fd2->setNetPrice(29.99); std::println("{}: netPrice = {}", fd2->getName(), fd2->getNetPrice); unique_ptr ch = std::make_unique ("PC Chassis"); ch->setNetPrice(39.99); ch->add(fd1); ch->add(fd2); std::println("{}: netPrice = {}", ch->getName(), ch->getNetPrice); fd2->add(fd1); } The program output is 3.5in Floppy: netPrice=19.99 5.25in Floppy: netPrice=29.99 PC Chassis: netPrice=89.97 terminate called after throwing an instance of 'std::runtime_error' what(): FloppyDisk::add ==See also== Perl Design Patterns Book Mixin Law of Demeter
References
- The Composite design pattern - Problem, Solution, and Applicability · retrieved 2017-08-11 · rev 1360477607
Cite this reference
MLA "The Composite design pattern - Problem, Solution, and Applicability." 2017-08-11. http://w3sdesign.com/?gr=s03&ugr=proble. Accessed 24 November 2026.
Chicago "The Composite design pattern - Problem, Solution, and Applicability." 2017-08-11. http://w3sdesign.com/?gr=s03&ugr=proble.
APA encyclopegia.org. (2017). The Composite design pattern - Problem, Solution, and Applicability. Retrieved November 24, 2026, from http://w3sdesign.com/?gr=s03&ugr=proble
- Scott Walters (2004). Perl Design Patterns Book . Archived from the original on 2016-03-08 . Retrieved 2010-01-18 . Wikipedia revision 1360477607, retrieved 2026-11-19 · retrieved 2010-01-17 · rev 1360477607
Cite this reference
MLA "Scott Walters (2004). Perl Design Patterns Book . Archived from the original on 2016-03-08 . Retrieved 2010-01-18 . Wikipedia revision 1360477607, retrieved 2026-11-19." Scott Walters (2004). 2010-01-17. https://web.archive.org/web/20160308182256/http://perldesignpatterns.com/?CompositePattern. Accessed 24 November 2026.
Chicago "Scott Walters (2004). Perl Design Patterns Book . Archived from the original on 2016-03-08 . Retrieved 2010-01-18 . Wikipedia revision 1360477607, retrieved 2026-11-19." Scott Walters (2004). 2010-01-17. https://web.archive.org/web/20160308182256/http://perldesignpatterns.com/?CompositePattern.
APA Scott Walters (2004). (2010). Scott Walters (2004). Perl Design Patterns Book . Archived from the original on 2016-03-08 . Retrieved 2010-01-18 . Wikipedia revision 1360477607, retrieved 2026-11-19. Retrieved November 24, 2026, from https://web.archive.org/web/20160308182256/http://perldesignpatterns.com/?CompositePattern
- The Composite design pattern - Structure and Collaboration · retrieved 2017-08-11 · rev 1360477607
Cite this reference
MLA "The Composite design pattern - Structure and Collaboration." 2017-08-11. http://w3sdesign.com/?gr=s03&ugr=struct. Accessed 24 November 2026.
Chicago "The Composite design pattern - Structure and Collaboration." 2017-08-11. http://w3sdesign.com/?gr=s03&ugr=struct.
APA encyclopegia.org. (2017). The Composite design pattern - Structure and Collaboration. Retrieved November 24, 2026, from http://w3sdesign.com/?gr=s03&ugr=struct
- The Composite design pattern - Implementation · retrieved 2017-08-11 · rev 1360477607
Cite this reference
MLA "The Composite design pattern - Implementation." 2017-08-11. http://w3sdesign.com/?gr=s03&ugr=implem. Accessed 24 November 2026.
Chicago "The Composite design pattern - Implementation." 2017-08-11. http://w3sdesign.com/?gr=s03&ugr=implem.
APA encyclopegia.org. (2017). The Composite design pattern - Implementation. Retrieved November 24, 2026, from http://w3sdesign.com/?gr=s03&ugr=implem
- A look at the Composite design pattern · retrieved 2020-07-19 · rev 1360477607
Cite this reference
MLA "A look at the Composite design pattern." Geary, David (13 September 2002). 2020-07-19. https://www.infoworld.com/article/2074564/a-look-at-the-composite-design-pattern.html. Accessed 24 November 2026.
Chicago "A look at the Composite design pattern." Geary, David (13 September 2002). 2020-07-19. https://www.infoworld.com/article/2074564/a-look-at-the-composite-design-pattern.html.
APA Geary, David (13 September 2002). (2020). A look at the Composite design pattern. Retrieved November 24, 2026, from https://www.infoworld.com/article/2074564/a-look-at-the-composite-design-pattern.html
- a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley. pp. 395 . ISBN 0-201-63361-2 . Wikipedia revision 1360477607, retrieved 2026-11-19 · retrieved 2026-11-18 · rev 1360477607
Cite this reference
MLA "a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley. pp. 395 . ISBN 0-201-63361-2 . Wikipedia revision 1360477607, retrieved 2026-11-19." a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. 2026-11-18. https://archive.org/details/designpatternsel00gamm/page/395. Accessed 24 November 2026.
Chicago "a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley. pp. 395 . ISBN 0-201-63361-2 . Wikipedia revision 1360477607, retrieved 2026-11-19." a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. 2026-11-18. https://archive.org/details/designpatternsel00gamm/page/395.
APA a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. (2026). a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley. pp. 395 . ISBN 0-201-63361-2 . Wikipedia revision 1360477607, retrieved 2026-11-19. Retrieved November 24, 2026, from https://archive.org/details/designpatternsel00gamm/page/395
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 163ff . ISBN 0-201-63361-2 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1360477607, retrieved 2026-11-19 · retrieved 2026-11-18 · rev 1360477607
Cite this reference
MLA "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 163ff . ISBN 0-201-63361-2 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1360477607, retrieved 2026-11-19." Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). 2026-11-18. https://archive.org/details/designpatternsel00gamm/page/163. Accessed 24 November 2026.
Chicago "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 163ff . ISBN 0-201-63361-2 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1360477607, retrieved 2026-11-19." Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). 2026-11-18. https://archive.org/details/designpatternsel00gamm/page/163.
APA Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). (2026). Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 163ff . ISBN 0-201-63361-2 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1360477607, retrieved 2026-11-19. Retrieved November 24, 2026, from https://archive.org/details/designpatternsel00gamm/page/163
Cite this article
MLA
"Composite pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/composite-pattern. Accessed 24 November 2026.
Chicago
"Composite pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/composite-pattern.
APA
encyclopegia.org. (2026). Composite pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/composite-pattern
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON