SlideShare ist ein Scribd-Unternehmen logo
1 von 12
1




Design Pattern –
Composite
Presented by Joncash
4/13/2012
2




Outline
•   Definition
•   Motivation
•   When to Use
•   Structure
•   Application
•   Example
3




Definition
• The composite pattern describes that a group of
  objects are to be treated in the same way as a
  single instance of an object.
• The intent of a composite is to "compose" objects
  into tree structures to represent part-whole
  hierarchies.
4




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, error prone.
5




When to Use
• 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
6




Structure
7




Classic Composite application
• ASP.NET
 ▫ TreeView
 ▫ System.Web.UI.Control
8




Example
static void Main()                 // Add and remove a leaf
{                                     Leaf leaf = new Leaf("Leaf D");
   // Create a tree structure         root.Add(leaf);
   Composite root = new               root.Remove(leaf);
   Composite("root");
   root.Add(new Leaf("Leaf A"));   // Recursively display tree
   root.Add(new Leaf("Leaf B"));      root.Display(1);

  Composite comp = new             // Wait for user
  Composite("Composite X");           Console.ReadKey();
  comp.Add(new Leaf("Leaf XA"));
  comp.Add(new Leaf("Leaf XB"));   }
   root.Add(comp);
   root.Add(new Leaf("Leaf C"));
9




Component
abstract class Component
{
  protected string name;
    // Constructor
    public Component(string name)
    {
      this.name = name;
    }
    public abstract void Add(Component c);
    public abstract void Remove(Component c);
    public abstract void Display(int depth);
}
10




Composite
class Composite : Component
{                                 public override void Remove(Component com
  private List<Component>          ponent)
  _children                       {
  = new List<Component>();          _children.Remove(component);
                                  }
 // Constructor
 public Composite(string name)    public override void Display(int depth)
   : base(name)                   {
 {                                  Console.WriteLine(new String('-', depth) +
 }                                 name);

public override void Add(Compon    // Recursively display child nodes
  ent component)                   foreach (Component component in _children
 {                                )
   _children.Add(component);       {
 }                                   component.Display(depth + 2);
                                   }
                                  }
11




Leaf
class Leaf : Component       public override void Add(Component c)
{                             {
  // Constructor                Console.WriteLine("Cannot add to a
  public Leaf(string name)    leaf");
    : base(name)              }
  {
  }                           public override void Remove(Component c)
                              {
                                Console.WriteLine("Cannot remove from
                              a leaf");
                              }

                              public override void Display(int depth)
                              {
                                Console.WriteLine(new String('-', depth) +
                              name);
                              }
12




Reference
• wiki/Composite_pattern
• dofactory.com/Patterns/PatternComposite

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Composite design pattern
Composite design patternComposite design pattern
Composite design pattern
 
Concept of Object Oriented Programming
Concept of Object Oriented Programming Concept of Object Oriented Programming
Concept of Object Oriented Programming
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
M04 Design Patterns
M04 Design PatternsM04 Design Patterns
M04 Design Patterns
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
encapsulation
encapsulationencapsulation
encapsulation
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Mediator
MediatorMediator
Mediator
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Object database standards, languages and design
Object database standards, languages and designObject database standards, languages and design
Object database standards, languages and design
 
Intake 38 data access 4
Intake 38 data access 4Intake 38 data access 4
Intake 38 data access 4
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Sda 8
Sda   8Sda   8
Sda 8
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Oops slide
Oops slide Oops slide
Oops slide
 
ch6
ch6ch6
ch6
 
Ashish oot
Ashish ootAshish oot
Ashish oot
 

Andere mochten auch

Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013
Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013
Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013FEDERICO ALMENARA CHECA
 
Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...
Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...
Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...Career Communications Group
 
DemoFest 2015 "Mission—JHome Content Manager"
DemoFest 2015 "Mission—JHome Content Manager"DemoFest 2015 "Mission—JHome Content Manager"
DemoFest 2015 "Mission—JHome Content Manager"Jeffery Goldman
 
Cuadernillo v termodinamica
Cuadernillo v termodinamicaCuadernillo v termodinamica
Cuadernillo v termodinamicaivan_antrax
 
Diario Resumen 20150821
Diario Resumen 20150821Diario Resumen 20150821
Diario Resumen 20150821Diario Resumen
 
Zeeshan Khan 2016 Cv 2
Zeeshan Khan 2016 Cv 2Zeeshan Khan 2016 Cv 2
Zeeshan Khan 2016 Cv 2Zeeshan Khan
 
Retail Institution Report
Retail Institution ReportRetail Institution Report
Retail Institution ReportFridz Felisco
 
Chave hh plastica v0 serie FK 233
Chave hh plastica v0 serie FK 233Chave hh plastica v0 serie FK 233
Chave hh plastica v0 serie FK 233Fernik
 
curl_multi_exec()でFire-and-forgetに失敗した話
curl_multi_exec()でFire-and-forgetに失敗した話curl_multi_exec()でFire-and-forgetに失敗した話
curl_multi_exec()でFire-and-forgetに失敗した話Takaaki Hirano
 

Andere mochten auch (14)

Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013
Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013
Guíon Eucaristía. Domingo XXIX del To. 20 de octubre del 2013
 
Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...
Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...
Unleash Your Potential! Successfully Navigate Your Way through Global and Vir...
 
CCP_SM
CCP_SMCCP_SM
CCP_SM
 
DemoFest 2015 "Mission—JHome Content Manager"
DemoFest 2015 "Mission—JHome Content Manager"DemoFest 2015 "Mission—JHome Content Manager"
DemoFest 2015 "Mission—JHome Content Manager"
 
Cuadernillo v termodinamica
Cuadernillo v termodinamicaCuadernillo v termodinamica
Cuadernillo v termodinamica
 
Diario Resumen 20150821
Diario Resumen 20150821Diario Resumen 20150821
Diario Resumen 20150821
 
Zeeshan Khan 2016 Cv 2
Zeeshan Khan 2016 Cv 2Zeeshan Khan 2016 Cv 2
Zeeshan Khan 2016 Cv 2
 
ALBY 1
ALBY 1ALBY 1
ALBY 1
 
Termodinamica trabajo resumen-ejercicios
Termodinamica trabajo  resumen-ejerciciosTermodinamica trabajo  resumen-ejercicios
Termodinamica trabajo resumen-ejercicios
 
Retail Institution Report
Retail Institution ReportRetail Institution Report
Retail Institution Report
 
Chave hh plastica v0 serie FK 233
Chave hh plastica v0 serie FK 233Chave hh plastica v0 serie FK 233
Chave hh plastica v0 serie FK 233
 
curl_multi_exec()でFire-and-forgetに失敗した話
curl_multi_exec()でFire-and-forgetに失敗した話curl_multi_exec()でFire-and-forgetに失敗した話
curl_multi_exec()でFire-and-forgetに失敗した話
 
El país de jesús
El país de jesúsEl país de jesús
El país de jesús
 
La ladrona de libros ficha didáctica
La ladrona de libros ficha didácticaLa ladrona de libros ficha didáctica
La ladrona de libros ficha didáctica
 

Ähnlich wie Design pattern composite 20120413 joncash 01

data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injectionsrmelody
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...go_oh
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingTricode (part of Dept)
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...yazad dumasia
 
Building maintainable web apps with Angular MS TechDays 2017
Building maintainable web apps with Angular MS TechDays 2017Building maintainable web apps with Angular MS TechDays 2017
Building maintainable web apps with Angular MS TechDays 2017Erik van Appeldoorn
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developementfrwebhelp
 

Ähnlich wie Design pattern composite 20120413 joncash 01 (20)

data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injection
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
Clean Code
Clean CodeClean Code
Clean Code
 
Play 2.0
Play 2.0Play 2.0
Play 2.0
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
 
Object
ObjectObject
Object
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Building maintainable web apps with Angular MS TechDays 2017
Building maintainable web apps with Angular MS TechDays 2017Building maintainable web apps with Angular MS TechDays 2017
Building maintainable web apps with Angular MS TechDays 2017
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Oops recap
Oops recapOops recap
Oops recap
 

Mehr von LearningTech

Mehr von LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Kürzlich hochgeladen

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Design pattern composite 20120413 joncash 01

  • 2. 2 Outline • Definition • Motivation • When to Use • Structure • Application • Example
  • 3. 3 Definition • The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. • The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
  • 4. 4 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, error prone.
  • 5. 5 When to Use • 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
  • 7. 7 Classic Composite application • ASP.NET ▫ TreeView ▫ System.Web.UI.Control
  • 8. 8 Example static void Main() // Add and remove a leaf { Leaf leaf = new Leaf("Leaf D"); // Create a tree structure root.Add(leaf); Composite root = new root.Remove(leaf); Composite("root"); root.Add(new Leaf("Leaf A")); // Recursively display tree root.Add(new Leaf("Leaf B")); root.Display(1); Composite comp = new // Wait for user Composite("Composite X"); Console.ReadKey(); comp.Add(new Leaf("Leaf XA")); comp.Add(new Leaf("Leaf XB")); } root.Add(comp); root.Add(new Leaf("Leaf C"));
  • 9. 9 Component abstract class Component { protected string name; // Constructor public Component(string name) { this.name = name; } public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth); }
  • 10. 10 Composite class Composite : Component { public override void Remove(Component com private List<Component> ponent) _children { = new List<Component>(); _children.Remove(component); } // Constructor public Composite(string name) public override void Display(int depth) : base(name) { { Console.WriteLine(new String('-', depth) + } name); public override void Add(Compon // Recursively display child nodes ent component) foreach (Component component in _children { ) _children.Add(component); { } component.Display(depth + 2); } }
  • 11. 11 Leaf class Leaf : Component public override void Add(Component c) { { // Constructor Console.WriteLine("Cannot add to a public Leaf(string name) leaf"); : base(name) } { } public override void Remove(Component c) { Console.WriteLine("Cannot remove from a leaf"); } public override void Display(int depth) { Console.WriteLine(new String('-', depth) + name); }

Hinweis der Redaktion

  1. Implementing the composite pattern lets clients treat individual objects and compositions uniformly 組合模式 : 將物件組合成樹形結再以表示「部份 - 整體」的層次結構 組合模式使得用戶對單個物件和組合物件的使用具有一致性。
  2. Composite can be used when clients should ignore the difference between compositions of objects and individual objects.