222 | Chapter 6: Composite Pattern
playlist called “eclectic” that includes both previous playlists and an additional song.
The following text output shows the song play sequence.
Queuing playlist: eclectic
Queuing playlist: drum licks
Queuing playlist: guitar riffs
playing bongo
playing tabla
playing djembe
playing ballad piano
playing acoustic guitar
playing electric guitar
When the “eclectic” playlist is queued for play, it queues and plays the embedded
“drum licks” and “guitar riffs” playlists as well. This is exactly the behavior we
expect from our composite playlist structure. You can easily remove songs and
playlists by implementing theremove( ) method as shown in the minimalist example.
Example: Animating Composite Objects Using Inverse Kinematics
There are many examples of excellent Flash games that use ActionScript to animate
characters on stage. Even a simple animated figure can have independently function-
ing body parts such as arms and legs that can be animated to jump, run, and kick.
Can we develop comple xanimated figures by treating body parts as composite and
component objects? Do we gain an advantage by being able to treat component and
composite parts of an animated figure in the same way? Indeed, the composite pat-
tern brings several advantages to animation, as will be evident by the animated snake
that will be developed in this example. Figure 6-6 shows a screenshot of the snake
that moves by means of inverse kinematics.
Figure 6-6. Snake constructed using a composite pattern