the composite pattern.. composite pattern intent –compose objects into tree structures to...

25
The Composite Pattern .

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

The Composite Pattern

.

Page 2: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Intent– Compose objects into tree structures to

represent part-whole hierarchies.– Composite lets clients treat individual objects

and compositions of objects uniformly.

Page 3: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Motivation Graphic applications let users build complex

diagrams out of simple components.

A simple implementation can define a class for primitives and other classes that act as containers for these.

Problem: The code that uses theses classes is forced to treat primitives and container objects differently.

Page 4: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Motivation:

- Using Composite pattern clients do not need to make this distinction.

- Graphic, Line, Text, Picture

The key: The abstract class that represent both primitives and their containers.

Page 5: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

Page 6: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

Page 7: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Applicability (use the pattern when…)– You want to represent part-whole hierarchies

of objects– You want clients to be able to ignore the

difference between compositions of objects and individual objects

Page 8: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite pattern

Structure:

Page 9: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Composite Object structure

Page 10: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern• Participants

– Component (Graphic)• Declares the interface for objects in the composition• Implements default behavior for the interface common to all classes, as

appropriate• Declares an interface for accessing and managing its child components• (Optional) Defines an interface for accessing a component’s parent in the

recursive structure, and implements it if that’s appropriate– Leaf (Line, Text, Rectangle)

• Represents the leaf objects in the composition. A leaf has no children• Defines behavior for primitive objects in the composition.

– Composite (Picture)• Defines behavior for components having children• Stores child components.• Implements child-related operations in the Component interface

– Client• Manipulates objects in the composition through the Component interface

Page 11: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Collaborations

Clients use the Component class interface to interact with all objects in the composite structures.

If the recipient is a leaf it is handled directly,

If it is a composite, it is passed to its children.

Page 12: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Consequences

1- Wherever client code expects a primitive, it can also take a composite object

2- Makes the client simple

3- Makes it easy to add new kinds of components

4- Disadvantage: Sometimes you want a composite to have only certain components, you can not rely on type system to enforce those restrictions.

Page 13: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern• Implementation

– Explicit Parent References The usual place to define the parent reference is in the component class. The

easiest way is to add and remove the reference when a leaf is added or removed from the composite

– Sharing Components• Share components to reduce storage requirements• Becomes difficult when? When a component can only have one parent• Which pattern makes this easier? Flyweight

– Maximizing the Component Interface• Component class should define as many common operations for the Composite and

Leaf classes as possible How returning children would be implemented for leaf for example?

– Declaring the Child Management Operations Trade off between:

• Transparency vs Uniformity• Safety Clients may try to do meaningless things like add and remove objects from

leaves• It is better let component fail and raise an exception if it is not allowed to add or remove

children

Page 14: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

Page 15: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern• Implementations (cont.)

– Should Component Implement a List of Components?– Child Ordering

• Sometimes it is useful to provide ordering. To do this just be careful when writing your Add/Remove children methods

• Iterator pattern (p257) comes in handy here– Caching to Improve Performance

• If caching is needed extra caching information should be stored in the Composite class

– Who Should Delete Components?• In a language without Garbage Collection its best to have Composite

responsible for deleting its children when it’s destroyed– What’s the Best Data Structure for Storing Components?

• Basically any will work (Linked List, trees, arrays, Hash Tables, etc)• The trick to choosing will be (as per usual) the efficiency trade-offs• Sometimes composites have a variable for each child, although this requires

each subclass of Composite to implement its own management interface. See Interpreter (p243) for an example.

Page 16: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Composite Pattern

• Known UsesAlmost all complex systems, MVC, every user interface

toolkit or framework• Related Patterns Decorator is often used with composite, Flyweight lets you share components, but they can not

refer to their parents Iterator can be used to traverse the composites Visitor localizes operations and behaviors that otherwise

be distributed across composite and leaf classes.

Page 17: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example1• Situation: A GUI system has window

objects which can contain various GUI components (widgets) such as, buttons and text areas. A window can also contain widget container objects which can hold other widgets.

• Solution 1: What if we designed all the widgets with different interfaces for "updating" the screen? We would then have to write a Window update() method as follows:

Page 18: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example1 public class Window {

Button[] buttons;Menu[] menus;TextArea[] textAreas;WidgetContainer[] containers;

public void update() { if (buttons != null)

for (int k = 0; k < buttons.length; k++)buttons[k].draw();

if (menus != null) for (int k = 0; k < menus.length; k++)

menus[k].refresh(); // Other widgets handled similarly.

if (containers != null) for (int k = 0; k < containers.length; k++ ) containers[k].updateWidgets(); } ...}

Page 19: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example 1• Well, that looks particularly bad. If we want to

add a new kind of widget, we have to modify the update() method of Window to handle it.

• Solution 2: We should always try to program to an interface, right? So, let's make all widgets support the Widget interface, either by being subclasses of a Widget class or implementing a Java Widget interface. Now our update() method becomes:

Page 20: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example1

Page 21: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example1

Page 22: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example1

Page 23: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example2

Page 24: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example2

Page 25: The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat

Example3- The Java AWT