ActionScript 3.0 Design Patterns

(Chris Devlin) #1

86 | Chapter 2: Factory Method Pattern


to implement default behavior common to all shape widgets in the definition. In


addition,ShapeWidgetalso subclassesSprite. The default behavior is defined in a


method calledsetLoc( )to set the X and Y coordinates of the sprite.ShapeWidgetalso


defines an abstract method calleddrawWidget( ) to draw the sprite.


Each Shape, Sprite, and MovieClip object has a property calledgraphicsthat is an


instance of theGraphicsclass. TheGraphicsclass includes properties and methods


for drawing and manipulating lines and shapes including fills, colors, and patterns.


The concrete classes derived from theShapeWidgetclass are listed in Examples 2-22


through 2-25. They draw the corresponding shapes in theconstructorusing methods


accessed through thegraphics property.


Example 2-21. ShapeWidget.as


package shapecreators
{
import flash.display.Sprite;


// ABSTRACT Class (should be subclassed and not instantiated)
internal class ShapeWidget extends Sprite
{
// ABSTRACT Method (should be implemented in subclass)
internal function drawWidget( ):void {}


internal function setLoc(xLoc:int, yLoc:int):void {
this.x = xLoc;
this.y = yLoc;
}
}
}


Example 2-22. SquareWidget.as


package shapecreators {


internal class SquareWidget extends ShapeWidget {


override internal function drawWidget( ):void
{
graphics.lineStyle(3, 0xFF00FF);
graphics.drawRect(-10, -10, 20, 20);
}
}
}

Free download pdf