10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

objects and <strong>com</strong>posite objects the same way. For example, when adding, or removing<br />

a <strong>com</strong>ponent, the client doesn’t have to bother with figuring out if the object is a<br />

primitive or <strong>com</strong>posite object. The client can just as easily remove the engine or a tire<br />

through a <strong>com</strong>mon interface.<br />

A useful way to understand the <strong>com</strong>posite pattern is to think of <strong>com</strong>plex<strong>com</strong>posite<br />

objects as hierarchical trees. We’re talking about upside-down trees as in Figure 6-1,<br />

where the system begins with a root node and cascades down, subdividing into several<br />

branches.<br />

Figure 6-1. A hierarchical tree structure showing nodes and leaves<br />

In Figure 6-1, the nodes that contain other <strong>com</strong>ponents are <strong>com</strong>posite objects. The<br />

leaf nodes are indivisible or primitive <strong>com</strong>ponents that cannot have any children.<br />

Each leaf node is a child of a <strong>com</strong>posite node and each <strong>com</strong>posite node can have<br />

multiple children, including other <strong>com</strong>posite nodes. Likewise, the <strong>com</strong>posite node<br />

that’s immediately up the hierarchy from a leaf is called its parent. As shown in the<br />

class diagram in Figure 6-2, the <strong>com</strong>posite pattern provides a <strong>com</strong>mon interface to<br />

deal with both <strong>com</strong>posite and leaf nodes.<br />

Client<br />

leaf nodes<br />

Leaf<br />

operation()<br />

Component<br />

add()<br />

remove(Component)<br />

getChild(int)<br />

operation()<br />

Figure 6-2. Class diagram of a <strong>com</strong>posite pattern<br />

root node (Composite node)<br />

Composite<br />

add()<br />

remove(Component)<br />

getChild(int)<br />

operation()<br />

children<br />

<strong>com</strong>posite nodes<br />

for each child in children<br />

child.operation()<br />

The <strong>com</strong>mon interface for both <strong>com</strong>posite and leaf nodes is the Component class. The<br />

<strong>com</strong>ponent class is usually defined as an abstract class. Therefore, the Component<br />

What Is the Composite Pattern? | 205

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!