Composite Pattern - PowerPoint PPT Presentation

About This Presentation
Title:

Composite Pattern

Description:

Composite Pattern – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 10
Provided by: Goog261
Category:
Tags: composite | dyd | ee | mt | pattern

less

Transcript and Presenter's Notes

Title: Composite Pattern


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
 
4
Implementation
  • interface Graphic //Prints the graphic.public
    void print()

5
 
  • / "Composite" /class CompositeGraphic
    implements Graphic //Collection of child
    graphics.private ListltGraphicgt mChildGraphics
    new ArrayListltGraphicgt()//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(gra
    phic)//Removes the graphic from the
    composition.public void remove(Graphic graphic)
    mChildGraphics.remove(graphic)

6
 
  • / "Leaf" /class Ellipse implements Graphic
    //Prints the graphic.public void print()
    System.out.println("Ellipse")

7
 
  • / 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(elli
    pse2)graphic1.add(ellipse3)graphic2.add(ellip
    se4)graphic.add(graphic1)graphic.add(graphic2
    )//Prints the complete graphic (four times the
    string "Ellipse").graphic.print()

8
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.

9
The End
  • Questions, concerns??
Write a Comment
User Comments (0)
About PowerShow.com