Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007ACS - 3913 Ron McFadyen1 Composite Pattern (see pages 353-367) A composite is a group of objects in which some objects contain others; one object.

Similar presentations


Presentation on theme: "Fall 2007ACS - 3913 Ron McFadyen1 Composite Pattern (see pages 353-367) A composite is a group of objects in which some objects contain others; one object."— Presentation transcript:

1 Fall 2007ACS - 3913 Ron McFadyen1 Composite Pattern (see pages 353-367) A composite is a group of objects in which some objects contain others; one object may represent groups, and another may represent an individual item, a leaf. The composite class diagram enables a tree structure – a hierarchy. The operations are appropriate for processing and traversing trees.

2 Fall 2007ACS - 3913 Ron McFadyen2 Composite Pattern Generic UML class diagram Component operation() Leaf operation() Composite operation() other() * Client Each node of the Component structure can respond to some common operation(s). I.e. the client can send the common operation to Component and the structure responds “appropriately”.

3 Fall 2007ACS - 3913 Ron McFadyen3 Composite Pattern Consider the Head First example What is the class diagram? Who is the client? What operations are defined for the component, the composite, and the leaf? How are they carried out? How are the associations implemented in the code?

4 Fall 2007ACS - 3913 Ron McFadyen4 Composite Pattern MenuComponent add() remove() getChild() print()… MenuItem print() Menu add() remove() getChild() print()… * Waitress What does it mean to ask a menu, or a menu item, to print itself?

5 Fall 2007ACS - 3913 Ron McFadyen5 Composite Pattern Menu menuComponents: ArrayList add() remove() getChild() print()… Note the text implements the association from Menu to MenuComponents with an array list data type. What is the implementation of print() in Menu and in MenuItem

6 Fall 2007ACS - 3913 Ron McFadyen6 Composite Pattern In the Menu class we have a print() that is appropriate for an internal node. public void print() { System.out.print("\n" + getName()); System.out.println(", " + getDescription()); System.out.println("---------------------"); Iterator iterator = menuComponents.iterator(); while (iterator.hasNext()) { MenuComponent menuComponent = (MenuComponent)iterator.next(); menuComponent.print(); }

7 Fall 2007ACS - 3913 Ron McFadyen7 Composite Pattern In the MenuItem class we have a print() that is appropriate for a leaf: public void print() { System.out.print(" " + getName()); if (isVegetarian()) { System.out.print("(v)"); } System.out.println(", " + getPrice()); System.out.println(" -- " + getDescription()); }

8 Fall 2007ACS - 3913 Ron McFadyen8 Composite Pattern We have just viewed the Head First implementation of print() methods appropriate for various nodes. Suppose you wanted nodes/methods to collaborate to count the total number of menu items. Say the waitress asks AllMenus “how many items are there?” How could that be done?

9 Fall 2007ACS - 3913 Ron McFadyen9 Composite Pattern A BCD RSTV WZ The tree on the left can be traversed in many ways: Pre-order traversal Visit a node and then its children: ABCRSWZTVD Post-order traversal Visit the children and then the parent: BRWZSTVCDA Level-order traversal Visit nodes on one level and then the next: ABCDRSTVWZ Which traversal is utilized in the Head First example? What tree does the Head First example instantiate?


Download ppt "Fall 2007ACS - 3913 Ron McFadyen1 Composite Pattern (see pages 353-367) A composite is a group of objects in which some objects contain others; one object."

Similar presentations


Ads by Google