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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

leaf 2<br />

Composite 1<br />

leaf 2<br />

leaf 3<br />

Display tree First child of second<br />

child of ‘root’ removed<br />

root<br />

Composite 2<br />

leaf 4 leaf 5<br />

leaf 6<br />

Second child<br />

of root<br />

First child of second<br />

child of root<br />

leaf 2<br />

Composite 1<br />

Second child<br />

of root<br />

Figure 6-5. Deleting <strong>com</strong>ponents from the tree structure<br />

Key OOP Concepts in the Composite Pattern<br />

The key OOP concept embedded in the <strong>com</strong>posite pattern is polymorphism. Polymorphism<br />

can be broadly defined as the ability of objects instantiated from different<br />

classes to respond to the same method calls in specific ways. This is possible because<br />

the method signature’s the same, even though the objects are instantiated from different<br />

classes. In short, the methods in the different classes show a <strong>com</strong>mon interface<br />

to clients.<br />

Since the <strong>com</strong>posite pattern allows clients to treat both leaf and <strong>com</strong>posite nodes the<br />

same way through a <strong>com</strong>mon interface, it’s a good example of polymorphism. This is<br />

due to both leaf and <strong>com</strong>posite classes implementing the same <strong>com</strong>ponent interface<br />

by subclassing the abstract <strong>com</strong>ponent class.<br />

Implementations of operations truly exemplify polymorphism. In the minimalist<br />

examples, the operation( ) method was implemented one way in the leaf nodes and<br />

another way in the <strong>com</strong>posite nodes. The <strong>com</strong>posite nodes have to forward the<br />

operation( ) call to all its child nodes. The operation( ) responds differently in both<br />

leaf and <strong>com</strong>posite nodes, but the client doesn’t need to know or care about these<br />

differences. The client doesn’t see the different implementations; it sees only the<br />

interface.<br />

Example: Music Playlists<br />

leaf 2<br />

leaf 4<br />

Have you created music playlists in your favorite digital music jukebox? If so, have<br />

you taken the next step and embedded playlists inside other playlists to create ever<br />

larger playlists? This is a perfect application to implement a <strong>com</strong>posite pattern. A<br />

library of digital music contains songs and playlists. A song is a primitive object,<br />

root<br />

Composite 2<br />

leaf 5<br />

leaf 6<br />

leaf 2<br />

root<br />

Composite 1 leaf 6<br />

leaf 2<br />

Second child of<br />

‘root’ removed<br />

Example: Music Playlists | 217

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

Saved successfully!

Ooh no, something went wrong!