Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Composite pattern
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Design pattern in software engineering}} In [[software engineering]], the '''composite pattern''' is a partitioning [[design pattern (computer science)|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.<ref name="GangOfFour">{{cite book |author = Gamma, Erich |author2 = Richard Helm |author3 = Ralph Johnson |author4 = John M. Vlissides |title = Design Patterns: Elements of Reusable Object-Oriented Software |publisher = Addison-Wesley |year = 1995 |pages = [https://archive.org/details/designpatternsel00gamm/page/395 395] |isbn = 0-201-63361-2 |url = https://archive.org/details/designpatternsel00gamm/page/395 }}</ref> ==Overview== {{Cleanup section|date=May 2024|reason=The subsection headers [[MOS:SECTIONSTYLE|should not redundantly refer back to the article title and should not be phrased as questions]]. But do these headers even accurately describe the subsection content? If yes, changing them to "Problems solved" and "Solution described" may be appropriate.}} The Composite<ref name="GoF">{{cite book|author=Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides|title=Design Patterns: Elements of Reusable Object-Oriented Software|year=1994|publisher=Addison Wesley|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/163 163ff]|url=https://archive.org/details/designpatternsel00gamm/page/163}}</ref> design pattern is one of the twenty-three well-known ''[[Design Patterns|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 design pattern can solve=== * 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) <code>Part</code> objects and (2) <code>Whole</code> objects that act as containers for <code>Part</code> objects, clients must treat them separately, which complicates client code.<ref>{{cite web|title=The Composite design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=s03&ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}</ref> ===Solutions the Composite design pattern describes=== * Define a unified <code>Component</code> interface for part (<code>Leaf</code>) objects and whole (<code>Composite</code>) objects. * Individual <code>Leaf</code> objects implement the <code>Component</code> interface directly, and <code>Composite</code> objects forward requests to their child components. This enables clients to work through the <code>Component</code> interface to treat <code>Leaf</code> and <code>Composite</code> objects uniformly: <code>Leaf</code> objects perform a request directly, and <code>Composite</code> 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. == Motivation == When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, more error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In [[object-oriented programming]], a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a "[[has-a]]" relationship between objects.<ref>{{cite book|url=http://perldesignpatterns.com/?CompositePattern|title=Perl Design Patterns Book|author=Scott Walters|year=2004|access-date=2010-01-18|archive-url=https://web.archive.org/web/20160308182256/http://perldesignpatterns.com/?CompositePattern|archive-date=2016-03-08|url-status=dead}}</ref> The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a [[least common denominator]] relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a group of shapes to have the same effect (in some sense) as resizing a single shape. == When to use == Composite should be used when clients ignore the difference between compositions of objects and individual objects.<ref name="GangOfFour" /> If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous. ==Structure== === UML class and object diagram === [[File:w3sDesign Composite Design Pattern UML.jpg|frame|none|A sample UML class and object diagram for the Composite design pattern. <ref>{{cite web|title=The Composite design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=s03&ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}</ref>]] In the above [[Unified Modeling Language|UML]] [[class diagram]], the <code>Client</code> class doesn't refer to the <code>Leaf</code> and <code>Composite</code> classes directly (separately). Instead, the <code>Client</code> refers to the common <code>Component</code> interface and can treat <code>Leaf</code> and <code>Composite</code> uniformly. <br> The <code>Leaf</code> class has no children and implements the <code>Component</code> interface directly. <br> The <code>Composite</code> class maintains a container of child <code>Component</code> objects (<code>children</code>) and forwards requests to these <code>children</code> (<code>for each child in children: child.operation()</code>). <br> The object collaboration diagram shows the run-time interactions: In this example, the <code>Client</code> object sends a request to the top-level <code>Composite</code> object (of type <code>Component</code>) in the tree structure. The request is forwarded to (performed on) all child <code>Component</code> objects (<code>Leaf</code> and <code>Composite</code> objects) downwards the tree structure.<br> ;Defining Child-Related Operations [[File:w3sDesign Composite Design Pattern Type Safety UML.jpg|frame|none|Defining child-related operations in the Composite design pattern. <ref>{{cite web|title=The Composite design pattern - Implementation|url=http://w3sdesign.com/?gr=s03&ugr=implem|website=w3sDesign.com|access-date=2017-08-12}}</ref>]] There are two design variants for defining and implementing child-related operations like adding/removing a child component to/from the container (<code>add(child)/remove(child)</code>) and accessing a child component (<code>getChild()</code>): * ''Design for uniformity:'' Child-related operations are defined in the <code>Component</code> interface. This enables clients to treat <code>Leaf</code> and <code>Composite</code> objects uniformly. But [[type safety]] is lost because clients can perform child-related operations on <code>Leaf</code> objects. * ''Design for type safety:'' Child-related operations are defined only in the <code>Composite</code> class. Clients must treat <code>Leaf</code> and <code>Composite</code> objects differently. But type safety is gained because clients ''cannot'' perform child-related operations on <code>Leaf</code> objects. The Composite design pattern emphasizes ''uniformity'' over ''type safety''. === UML class diagram === [[Image:Composite UML class diagram (fixed).svg|thumb|none|600px|Composite pattern in [[Unified Modeling Language|UML]].]] ;Component * is the abstraction for all components, including composite ones * declares the interface for objects in the composition * (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate ;Leaf * represents leaf objects in the composition * implements all Component methods ;Composite * represents a composite Component (component having children) * implements methods to manipulate children * implements all Component methods, generally by delegating them to its children [[Image:Composite pattern in LePUS3.png|thumb|none|400px|Composite pattern in [[Lepus3|LePUS3]].]] == Variation == As it is described in [[Design Patterns]], the pattern also involves including the child-manipulation methods in the main Component interface, not just the Composite subclass. More recent descriptions sometimes omit these methods.<ref>{{cite web |last1=Geary |first1=David |date=2002-09-13 |df=dmy |url=https://www.infoworld.com/article/2074564/a-look-at-the-composite-design-pattern.html |title=A look at the Composite design pattern |department=Java Design Patterns |work=[[JavaWorld]] |access-date=2020-07-20}}</ref> == Example == <!-- Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language under the sun. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Composite --> This C++14 implementation is based on the pre C++98 implementation in the book. <syntaxhighlight lang="c++"> #include <iostream> #include <string> #include <list> #include <memory> #include <stdexcept> typedef double Currency; // declares the interface for objects in the composition. class Equipment { // Component public: // implements default behavior for the interface common to all classes, as appropriate. virtual const std::string& getName() { return name; } virtual void setName(const std::string& name_) { name = name_; } virtual Currency getNetPrice() { return netPrice; } virtual void setNetPrice(Currency netPrice_) { netPrice = netPrice_; } // declares an interface for accessing and managing its child components. virtual void add(std::shared_ptr<Equipment>) = 0; virtual void remove(std::shared_ptr<Equipment>) = 0; virtual ~Equipment() = default; protected: Equipment() :name(""), netPrice(0) {} Equipment(const std::string& name_) :name(name_), netPrice(0) {} private: std::string name; Currency netPrice; }; // defines behavior for components having children. class CompositeEquipment : public Equipment { // Composite public: // implements child-related operations in the Component interface. virtual Currency getNetPrice() override { Currency total = Equipment::getNetPrice(); for (const auto& i:equipment) { total += i->getNetPrice(); } return total; } virtual void add(std::shared_ptr<Equipment> equipment_) override { equipment.push_front(equipment_.get()); } virtual void remove(std::shared_ptr<Equipment> equipment_) override { equipment.remove(equipment_.get()); } protected: CompositeEquipment() :equipment() {} CompositeEquipment(const std::string& name_) :equipment() { setName(name_); } private: // stores child components. std::list<Equipment*> equipment; }; // represents leaf objects in the composition. class FloppyDisk : public Equipment { // Leaf public: FloppyDisk(const std::string& name_) { setName(name_); } // A leaf has no children. void add(std::shared_ptr<Equipment>) override { throw std::runtime_error("FloppyDisk::add"); } void remove(std::shared_ptr<Equipment>) override { throw std::runtime_error("FloppyDisk::remove"); } }; class Chassis : public CompositeEquipment { public: Chassis(const std::string& name_) { setName(name_); } }; int main() { // The smart pointers prevent memory leaks. std::shared_ptr<FloppyDisk> fd1 = std::make_shared<FloppyDisk>("3.5in Floppy"); fd1->setNetPrice(19.99); std::cout << fd1->getName() << ": netPrice=" << fd1->getNetPrice() << '\n'; std::shared_ptr<FloppyDisk> fd2 = std::make_shared<FloppyDisk>("5.25in Floppy"); fd2->setNetPrice(29.99); std::cout << fd2->getName() << ": netPrice=" << fd2->getNetPrice() << '\n'; std::unique_ptr<Chassis> ch = std::make_unique<Chassis>("PC Chassis"); ch->setNetPrice(39.99); ch->add(fd1); ch->add(fd2); std::cout << ch->getName() << ": netPrice=" << ch->getNetPrice() << '\n'; fd2->add(fd1); } </syntaxhighlight> The program output is <syntaxhighlight lang="c++"> 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 </syntaxhighlight> ==See also== *[[Perl Design Patterns Book]] *[[Mixin]] *[[Law of Demeter]] == References == {{reflist}} == External links == {{wikibooks|Computer Science Design Patterns|Composite|Composite implementations in various languages}} *[http://designpattern.co.il/Composite.html Composite Pattern] implementation in Java *[[c2:CompositePattern|Composite pattern description from the Portland Pattern Repository]] *[https://web.archive.org/web/20151002170520/http://www.lepus.org.uk/ref/companion/Composite.xml Composite pattern in UML and in LePUS3, a formal modelling language] *[http://search.cpan.org/dist/Class-Delegation/lib/Class/Delegation.pm Class::Delegation on CPAN] *[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/149878 "The End of Inheritance: Automatic Run-time Interface Building for Aggregated Objects"] by [[Paul Baranowski]] *[http://perfectjpattern.sourceforge.net/dp-composite.html PerfectJPattern Open Source Project], Provides componentized implementation of the Composite Pattern in Java *[https://web.archive.org/web/20090531030742/http://www.theresearchkitchen.com/blog/archives/57] A persistent Java-based implementation *[http://sourcemaking.com/design_patterns/composite Composite Design Pattern] {{Design Patterns Patterns}} {{DEFAULTSORT:Composite Pattern}} [[Category:Software design patterns]] [[Category:Articles with example Java code]] [[Category:Articles with example C Sharp code]]
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Cleanup section
(
edit
)
Template:Design Patterns Patterns
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Wikibooks
(
edit
)
Search
Search
Editing
Composite pattern
Add topic