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
(section)
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!
== 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>
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)
Search
Search
Editing
Composite pattern
(section)
Add topic