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.

Example 6-1. Component.as (continued)<br />

}<br />

}<br />

}<br />

throw new IllegalOperationError<br />

("remove operation not supported");<br />

public function getChild(n:int):Component<br />

{<br />

throw new IllegalOperationError<br />

("getChild operation not supported");<br />

return null;<br />

}<br />

// ABSTRACT Method (must be overridden in a subclass)<br />

public function operation( ):void {}<br />

The Component class should behave as an abstract class and should not be instantiated.<br />

It also defines the abstract interface for managing child <strong>com</strong>ponents, and provides<br />

default implementations for the add(c:Component), remove(c:Component) and<br />

getChild(n:int) methods. The default implementations for these methods are<br />

designed for leaf nodes and will raise an exception by throwing an<br />

IllegalOperationError. This should be the case as leaf nodes cannot have children<br />

and should not implement operations that deal with child nodes. In addition, the<br />

operation( ) method is defined as an abstract method without implementation. It is<br />

left up to the classes that subclass Component to provide an implementation for it.<br />

Example 6-2. Leaf.as<br />

package<br />

{<br />

public class Leaf extends Component<br />

{<br />

private var sName:String;<br />

}<br />

}<br />

public function Leaf(sNodeName:String)<br />

{<br />

this.sName = sNodeName;<br />

}<br />

override public function operation( ):void<br />

{<br />

trace(this.sName);<br />

}<br />

The Leaf class extends the Component class. It declares a property called sName that<br />

holds the name of the leaf. It also implements a parameterized constructor that takes<br />

a string value that’s then set to the sName property. It implements the operation( )<br />

208 | Chapter 6: Composite Pattern

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

Saved successfully!

Ooh no, something went wrong!