O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Minimalist Example of a Composite Pattern

This example implements the composite shown in the class diagram in Figure 6-2. The Component.as file (Example 4-17) contains the Component abstract class that defines the interface for both leaf and composite nodes. The Leaf.as file (Example 6-2) contains the Leaf class, and the Composite.as file (Example 6-3) contains the Composite class. Both Leaf and Composite classes extend the Component class and provide necessary implementations. The Main.as file (Example 2-7) contains the client class Main (also known as the document class for the Flash document).

Example 6-1. 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 operation():void {}

    }
}

The Component class should behave as an abstract class and should not be instantiated. It also defines the abstract interface for managing child components, and provides default implementations for the add(c:Component), remove(c:Component) and getChild(n:int) methods. The default implementations ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required