SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de LearningTech

Mais de 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
 

Último

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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
 
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
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Último (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

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); }

Notas do Editor

  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.