218 | Chapter 6: Composite Pattern
while a playlist is a composite object that contains a collection of songs. Let’s first
create the component class for our playlist example application.
TheComponent.as file (Example 6-8) contains the Component abstract class that
defines the interface for both songs and playlists. TheSong.asfile (Example 6-10)
contains theSongclass, and thePlaylist.asfile (Example 6-9) contains thePlaylist
class. BothSongandPlaylistclasses extend theComponentclass and provide neces-
sary implementations.
TheComponentclass defines the abstract interface for both theSongandPlaylist
classes. It also defines the abstract methodplay( ).
Example 6-8. Component.as
package
{
import flash.errors.IllegalOperationError;
// ABSTRACT Class (should be subclassed and not instantiated)
public class Component
{
public function add(c:Component):void
{
throw new IllegalOperationError
("add operation not supported");
}
public function remove(c:Component):void
{
throw new IllegalOperationError
("remove operation not supported");
}
public function getChild(n:int):Component
{
throw new IllegalOperationError
("getChild operation not supported");
return null;
}
// ABSTRACT Method (must be overridden in a subclass)
public function play( ):void {}
}
}
Example 6-9. Playlist.as
package
{
public class Playlist extends Component
{
private var sName:String;