SlideShare a Scribd company logo
1 of 1
Composite Design Pattern
The Composite Design pattern allows a client object to treat both single components and
collections of components identically.In the UML class diagram below, the Client uses an
abstract component, AComponent, for some abstract task, operation(). At run-time, the
Client holds a reference to a concrete component such as Leaf1 or Leaf2. When the operation
task is requested by the Client, the specific concrete behavior with the particular concrete
component referenced will be performed.
The Composite class is a concrete component like Leaf1 and Leaf2, but has no operation()
behavior of its own.     Instead, Composite is composed with a collection of other abstract
components, which may be of any other concrete component type including the composite
itself. The unifying fact is that they are all abstractly AComponents. When the operation()
method of a Composite object is called, it simply dispatches the request sequentially to all of its
quot;childrenquot; components.      For instance, a Composite object could hold references to both a
Leaf1 and a Leaf2 instance. If a client holds a reference to that Composite object and calls its
operation() method, the Composite object will first call operation on its Leaf1 instance and then
operation() on its Leaf2 instance. Thus composite behavior of Leaf1 plus Leaf2 behaviors is
achieved without either duplicating code or by having the Client object knowing that the two leaf
components were involved.




Composite patterns are often used to represent recursive data structures. The recursive nature
of the Composite structure naturally gives way to recursive code to process that structure.
Note: The collection of AComponents held by Composite, quot;childrenquot;, is shown above as an
array. However, the behavior of a Composite pattern is independent of exactly how the
collection of AComponents is implemented. If access speed is not an issue, a vector or a list
may be a better implementation choice.   The addChild() and removeChild() methods are
optional.
In Design Patterns, the abstract component AComponent is shown as having accessor methods
for child AComponents. They are not shown here because it is debatable as to whether one
wants the Client to fundamentally view the AComponent as a single component or as a collection
of components. Design Patterns models all AComponents as collections while the above
design models them all as single components. The exact nature of those accessor methods is
also debatable.

More Related Content

What's hot

Intake 38 data access 4
Intake 38 data access 4Intake 38 data access 4
Intake 38 data access 4Mahmoud Ouf
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structureMahmoud Alfarra
 
Linking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scriptsLinking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scriptsKhalid Belhajjame
 
Modeling of multiversion concurrency control
Modeling of multiversion concurrency controlModeling of multiversion concurrency control
Modeling of multiversion concurrency controlJawid Ahmad Baktash
 
Kajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third YearKajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third YearDezyneecole
 
Lexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP programLexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP programIOSR Journals
 
Relational Algebra Operations
Relational Algebra OperationsRelational Algebra Operations
Relational Algebra OperationsShefa Idrees
 
Software engg. pressman_ch-11
Software engg. pressman_ch-11Software engg. pressman_ch-11
Software engg. pressman_ch-11Dhairya Joshi
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and AssociativityNicole Ynne Estabillo
 
Relation Algebra in MS SQL | Types of relation Alger-bra
Relation Algebra in MS SQL | Types of relation Alger-bra Relation Algebra in MS SQL | Types of relation Alger-bra
Relation Algebra in MS SQL | Types of relation Alger-bra khalidsheikh24
 
Reactive cocoa
Reactive cocoaReactive cocoa
Reactive cocoagillygize
 

What's hot (20)

Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Partial Functions In Scala
Partial Functions In ScalaPartial Functions In Scala
Partial Functions In Scala
 
Intake 38 data access 4
Intake 38 data access 4Intake 38 data access 4
Intake 38 data access 4
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structure
 
Algorithms Analysis
Algorithms Analysis Algorithms Analysis
Algorithms Analysis
 
Linking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scriptsLinking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scripts
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Modeling of multiversion concurrency control
Modeling of multiversion concurrency controlModeling of multiversion concurrency control
Modeling of multiversion concurrency control
 
Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Kajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third YearKajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third Year
 
Lexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP programLexical and Parser tool for CBOOP program
Lexical and Parser tool for CBOOP program
 
Relational Algebra Operations
Relational Algebra OperationsRelational Algebra Operations
Relational Algebra Operations
 
Software engg. pressman_ch-11
Software engg. pressman_ch-11Software engg. pressman_ch-11
Software engg. pressman_ch-11
 
Ikc 2015
Ikc 2015Ikc 2015
Ikc 2015
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
 
Control statements
Control statementsControl statements
Control statements
 
Relation Algebra in MS SQL | Types of relation Alger-bra
Relation Algebra in MS SQL | Types of relation Alger-bra Relation Algebra in MS SQL | Types of relation Alger-bra
Relation Algebra in MS SQL | Types of relation Alger-bra
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1
 
Reactive cocoa
Reactive cocoaReactive cocoa
Reactive cocoa
 

Similar to Composite Design Pattern

CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III pkaviya
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...Philip Schwarz
 
Modeling Aspects with AP&P Components
Modeling Aspects with AP&P ComponentsModeling Aspects with AP&P Components
Modeling Aspects with AP&P Componentsmukhtarhudaya
 
Maxbox starter19
Maxbox starter19Maxbox starter19
Maxbox starter19Max Kleiner
 
Pertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagramPertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagramAbi Bobon
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design PatternsDang Tuan
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming PrinciplesAndrii Lundiak
 
Design pattern (week 2)
Design pattern (week 2)Design pattern (week 2)
Design pattern (week 2)stanbridge
 
Unified Modeling Language
Unified Modeling LanguageUnified Modeling Language
Unified Modeling Languagesurana college
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
Lecture 12
Lecture 12Lecture 12
Lecture 12Rana Ali
 
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxSIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxedgar6wallace88877
 
C questions
C questionsC questions
C questionsparm112
 

Similar to Composite Design Pattern (20)

Visitor design patterns
Visitor design patternsVisitor design patterns
Visitor design patterns
 
CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III CS8592 Object Oriented Analysis & Design - UNIT III
CS8592 Object Oriented Analysis & Design - UNIT III
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
Ch14
Ch14Ch14
Ch14
 
Modeling Aspects with AP&P Components
Modeling Aspects with AP&P ComponentsModeling Aspects with AP&P Components
Modeling Aspects with AP&P Components
 
Uml
UmlUml
Uml
 
Maxbox starter19
Maxbox starter19Maxbox starter19
Maxbox starter19
 
Intro Uml
Intro UmlIntro Uml
Intro Uml
 
Pertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagramPertemuan 6-2-sequence-diagram
Pertemuan 6-2-sequence-diagram
 
Slides chapter 11
Slides chapter 11Slides chapter 11
Slides chapter 11
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
Design pattern (week 2)
Design pattern (week 2)Design pattern (week 2)
Design pattern (week 2)
 
Unified Modeling Language
Unified Modeling LanguageUnified Modeling Language
Unified Modeling Language
 
Memory management
Memory managementMemory management
Memory management
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxSIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
 
C questions
C questionsC questions
C questions
 

Recently uploaded

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Composite Design Pattern

  • 1. Composite Design Pattern The Composite Design pattern allows a client object to treat both single components and collections of components identically.In the UML class diagram below, the Client uses an abstract component, AComponent, for some abstract task, operation(). At run-time, the Client holds a reference to a concrete component such as Leaf1 or Leaf2. When the operation task is requested by the Client, the specific concrete behavior with the particular concrete component referenced will be performed. The Composite class is a concrete component like Leaf1 and Leaf2, but has no operation() behavior of its own. Instead, Composite is composed with a collection of other abstract components, which may be of any other concrete component type including the composite itself. The unifying fact is that they are all abstractly AComponents. When the operation() method of a Composite object is called, it simply dispatches the request sequentially to all of its quot;childrenquot; components. For instance, a Composite object could hold references to both a Leaf1 and a Leaf2 instance. If a client holds a reference to that Composite object and calls its operation() method, the Composite object will first call operation on its Leaf1 instance and then operation() on its Leaf2 instance. Thus composite behavior of Leaf1 plus Leaf2 behaviors is achieved without either duplicating code or by having the Client object knowing that the two leaf components were involved. Composite patterns are often used to represent recursive data structures. The recursive nature of the Composite structure naturally gives way to recursive code to process that structure. Note: The collection of AComponents held by Composite, quot;childrenquot;, is shown above as an array. However, the behavior of a Composite pattern is independent of exactly how the collection of AComponents is implemented. If access speed is not an issue, a vector or a list may be a better implementation choice. The addChild() and removeChild() methods are optional. In Design Patterns, the abstract component AComponent is shown as having accessor methods for child AComponents. They are not shown here because it is debatable as to whether one wants the Client to fundamentally view the AComponent as a single component or as a collection of components. Design Patterns models all AComponents as collections while the above design models them all as single components. The exact nature of those accessor methods is also debatable.