SlideServe
Menu
Préférences
© SlideServe · v2026

Composite Pattern in Object-Oriented Design

vdean avatar
9 diapositives · 117 Views · Dec 20, 2019

Learn about the Composite pattern that represents objects in tree structures and allows treating individual and composite objects uniformly. Implement the pattern in Java with examples.

Composite Pattern in Object-Oriented Design - slide 1
1 / 9

Télécharger la présentation

Composite Pattern in Object-Oriented Design
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Composite Pattern Yevgeny Keyser

  2. What is Composite? • Object Structural Pattern • Tree organization • Key ideas • Aggregation/composition • Divide And Conquer(recursion) • Uniformity • Individual/composite objects have the same interface

  3. Implementation interface Graphic {//Prints the graphic.public void print();}

  4. /** "Composite" */class CompositeGraphic implements Graphic {//Collection of child graphics.private List<Graphic> mChildGraphics = new ArrayList<Graphic>();//Prints the graphic.public void print() {for (Graphic graphic : mChildGraphics) {graphic.print();}}//Adds the graphic to the composition.public void add(Graphic graphic) {mChildGraphics.add(graphic);}//Removes the graphic from the composition.public void remove(Graphic graphic) {mChildGraphics.remove(graphic);}}

  5. /** "Leaf" */class Ellipse implements Graphic {//Prints the graphic.public void print() {System.out.println("Ellipse");}}

  6. /** Client */public class Program {public static void main(String[] args) {//Initialize four ellipsesEllipse ellipse1 = new Ellipse();Ellipse ellipse2 = new Ellipse();Ellipse ellipse3 = new Ellipse();Ellipse ellipse4 = new Ellipse();//Initialize three composite graphicsCompositeGraphic graphic = new CompositeGraphic();CompositeGraphic graphic1 = new CompositeGraphic();CompositeGraphic graphic2 = new CompositeGraphic();//Composes the graphicsgraphic1.add(ellipse1);graphic1.add(ellipse2);graphic1.add(ellipse3);graphic2.add(ellipse4);graphic.add(graphic1);graphic.add(graphic2);//Prints the complete graphic (four times the string "Ellipse").graphic.print();}}

  7. Extensions? • From Tree to Graph • Issues to keep in mind: • Multiple parents • Looping • Redundant storage • Mitigation • Flyweight pattern(lookup cache) to reuse objects • Depth-First Search coloring to prevent loops.

  8. The End Questions, concerns??