Advanced ActionScript 3: Design Patterns, Second Edition (2015)
Chapter 9. Q&A
The previous chapters on design patterns have covered an enormous amount, and now you get an opportunity to use that information. This chapter provides a quiz consisting of 25 questions, to provide you with immediate feedback on your understanding of the material. These questions pertain to design patterns (creational, behavioral and structural) as covered in this book.
You won’t be scored or judged on your answers, so do your best to use this chapter as a way to further understand and practice using design patterns.
Notes: 1. For formatting reasons, the quiz questions don’t include packages. External definitions must always have a package keyword. 2. Some questions are straightforward, and others require you to supply the implementations for incomplete listings.
Design Pattern Quiz
1. The common prefixes make, create, and get reflect which specific design pattern?
____________________
2. Primitive operations, which are prefixed with do, are used by which pattern?
____________________
3. Separate the instantiation from its assembly to promote flexibility in Listing 9-1.
Listing 9-1. AbstractClass
public class AbstractClass extends Sprite
{
public function AbstractClass()
{
var someMovieClip : MovieClip = new ConcreteMovieClip();
someMovieClip.y = 25;
someMovieClip.x = 40;
someMovieClip.play();
addChild( someMovieClip );
}
}
Listing 9-2. AbstractClass
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-3. FactoryMethodClass.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
4. Design a decorator that enhances a bitmap with a scrollRect that can scroll to reveal concealed areas of the image using the mouse. The bitmap interface is IBitmap in Listing 9-4.
Listing 9-4. IBitmap
public interface IBitmap
{
function get bitmapData() : BitmapData;
function set bitmapData( value : BitmapData ) : void;
function get pixelSnapping() : String;
function set pixelSnapping( value : String ) : void;
function get smoothing() : Boolean;
function set smoothing( value : Boolean ) : void;
}
Listing 9-5. DecoratorAbstract.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-6. MouseScrollingDecorator.as
________________________________________________________________
________________________________________________________________
protected var _viewport : Rectangle;
________________________________________________________________
protected var _pixelsPerWide : int;
________________________________________________________________
protected var _pixelsPerTall : int;
________________________________________________________________
private var _rectTall : int = 400;
________________________________________________________________
private var _rectWide : int = 400;
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
5. Explain how you can subclass a method and provide it with an implementation without disturbing the algorithm in which the implementation is required.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
6. Explain the benefits of an Iterator in an aggregate.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
7. Define a component interface that favors transparency over safety in all of its components.
Listing 9-7. IComponent.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
8. This pattern ensures state synchronization. ____________________
9. Mushroomy Kingdom uses the console platform and its power to revisit stage 1-1 with dreamy textures. Table 9-1 lists the names that reference the linked images of the .fla.
Table 9-1. Stage 1-1 concretes revisited
New stone floor tile: StoneFlooring |
New money box tile: MoneyBox |
New brick tile: WhiteStone |
New pipe tile: IndustrialPlumbing |
Cloud: AlphaCloud |
Hill terrain: HillSide |
Using the AbstractMarioLevelDirector and the AbstractMarioEsqueLevelEditor code and the reference names from Table 9-1, write the implementations to populate this scene. The dimensions aren’t important.
Listing 9-8. AbstractMarioEsqueLevelEditor.as
public class AbstractMarioEsqueLevelEditor
{
private var _bitmapD : BitmapData;
private var _backgroundColor : uint;
private var _width : int;
private var _height : int;
private var _pt : Point;
private var _tile : Shape;
public function AbstractMarioEsqueLevelEditor()
{
_tile = new Shape();
_pt = new Point( 0 , 0 );
}
final public function createMap() : void
{
bitmap = doCreateMap();
}
final public function getLevel() : BitmapData
{
return _bitmapD;
}
final public function createStone( rect : Rectangle ) : void
{
addTile( doCreateStone() , rect );
}
final public function createSolidBrick( rect : Rectangle ) : void
{
addTile( doCreateSolidBrick() , rect );
}
final public function createBreakableBrick( rect : Rectangle ) : void
{
addTile( doCreateBreakableBrick() , rect );
}
final public function createMoneyBox( rect : Rectangle ) : void
{
addTile( doCreateMoneyBox() , rect );
}
final public function createCloud( rect : Rectangle ) : void
{
addTile( doCreateCloud() , rect );
}
final public function createHill( rect : Rectangle ) : void
{
addTile( doCreateHill() , rect );
}
final public function createBush( rect : Rectangle ) : void
{
addTile( doCreateBush() , rect );
}
final public function createCastle( rect : Rectangle ) : void
{
addTile( doCreateCastle() , rect );
}
final public function createPipe( rect : Rectangle ) : void
{
addTile( doCreatePipe() , rect );
}
final public function get width() : int
{
return _width;
}
final public function set width( width : int ) : void
{
_width = width;
}
final public function get height() : int
{
return _height;
}
final public function set height( height : int ) : void
{
_height = height;
}
final public function get backgroundColor() : uint
{
return _backgroundColor;
}
final public function set backgroundColor( backgroundColor : uint ) : void
{
_backgroundColor = backgroundColor;
}
final public function get bitmap() : BitmapData
{
return _bitmapD;
}
final public function set bitmap( bitmap : BitmapData ) : void
{
_bitmapD = bitmap;
}
protected function doCreateMap() : BitmapData
{
return new BitmapData( width , height , false , backgroundColor );
}
protected function doCreateStone() : DisplayObject
{
throw new IllegalOperationError( 'doCreateStone must be overridden' );
return null;
}
protected function doCreateSolidBrick() : DisplayObject
{
throw new IllegalOperationError( 'doCreateSolidBrick must be overridden' );
return null;
}
protected function doCreateBreakableBrick() : DisplayObject
{
throw new IllegalOperationError( 'doCreateBreakableBrick must be overridden' );
return null;
}
protected function doCreateMoneyBox() : DisplayObject
{
throw new IllegalOperationError( 'doCreateMoneyBox must be overridden' );
return null;
}
protected function doCreateCloud() : DisplayObject
{
throw new IllegalOperationError( 'doCreateCloud must be overridden' );
return null;
}
protected function doCreateHill() : DisplayObject
{
throw new IllegalOperationError( 'doCreateHill must be overridden' );
return null;
}
protected function doCreateBush() : DisplayObject
{
throw new IllegalOperationError( 'doCreateBush must be overridden' );
return null;
}
protected function doCreateCastle() : DisplayObject
{
throw new IllegalOperationError( 'doCreateCastle must be overridden' );
return null;
}
protected function doCreatePipe() : DisplayObject
{
throw new IllegalOperationError( 'doCreatePipe must be overridden' );
return null;
}
private function addTile( dO : DisplayObject , rect : Rectangle ) : void
{
var sprite : BitmapData = snapShot( dO );
_pt.x = rect.x;
_pt.y = rect.y;
if (rect.width > 0 || rect.height > 0)
{
sprite = tile( sprite , rect );
}
bitmap.copyPixels( sprite , sprite.rect , _pt );
}
private function snapShot( dO : DisplayObject ) : BitmapData
{
var snapshot : BitmapData = new BitmapData( dO.width , dO.height , true , 0 );
snapshot.draw( dO );
return snapshot;
}
private function tile( bmpd : BitmapData , rect : Rectangle ) : BitmapData
{
var _t : Shape = _tile;
var g : Graphics = _t.graphics;
g.clear();
g.beginBitmapFill( bmpd , null , true , false );
g.drawRect( 0 , 0 , rect.width , rect.height );
g.endFill();
return snapShot( _t );
}
}
Listing 9-9. QuizLevelEditor.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-10. AbstractMarioLevelDirector.as
public class AbstractMarioLevelDirector
{
protected const _width : int = 400;
protected const _height : int = 300;
protected const _bgColor : uint = 0xacccff;
protected var _builder : AbstractMarioEsqueLevelEditor;
public function AbstractMarioLevelDirector( builder:AbstractMarioEsqueLevelEditor );
{
_builder = builder;
}
public function getLevel() : BitmapData
{
return _builder.getLevel();
}
}
Listing 9-11. QuizLevelDirector.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
10.Explain why it’s unwise to use a Simple Singleton in an application.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
11.The following code is from an unrevealed class.
Listing 9-12. Unrevealed class
...cont
public function makeFastFoodSandwich( menu_number : int ) : ValueMeal
{
switch(menu_number)
{
case 1:
return new DoubleStack();
break;
case 2 :
return new ChickenSandwich();
break;
case 3:
return new ChickenNuggets();
break;
case 4:
return new Frosty();
break;
}
}
...cont
The code in Listing 9-12 uses the Factory Method pattern? True False
12.Twitter is the epitome of which design pattern? ____________________
13.Having parallel hierarchies means you use fewer classes than when using orthogonal hierarchies. True False
14.These three patterns can optionally intercept a request before passing it on.
____________________
____________________
____________________
15.Show a loose composite that stops all MovieClips in the DisplayList.
//traverse(this.stage);
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
16.Re-create the display list from AS3 as a composite.
Listing 9-13. IComponent.as (DisplayObject) interface
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-14. IComposite.as (DisplayObjectContainer) interface
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-15. Leaf.as (DisplayObject)
public class Leaf implements __________________
{
}
17.What are the two most significant differences between the State pattern and the Strategy pattern?
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
18.Suppose a loader uses the following states: Closed, OpeningConnection, Loading, and Loaded. Given the interface of ILoader shown in Listing 9-16, assemble a loader using only the State pattern, ensuring that the loader can load a new request at any given point in time, as well as be destroyed, without using any conditional statements.
Listing 9-16. Loader interface
public interface ILoader
{
function close();
function load( request : URLRequest , context : LoaderContext = null ) : void;
function loadBytes( bytes : ByteArray , context : LoaderContext = null ) : void;
function get content() : DisplayObject;
function get contentLoaderInfo() : LoaderInfo;
function get ldr() : Loader;
function dispose() : void;
}
Listing 9-17. AbstractLoadersContext.as
public class AbstractLoadersContext extends Sprite implements ILoader
_______________________________________________________________
{
_______________________________________________________________
private var _ldr : Loader;
_______________________________________________________________
protected var _stateLoader : ALoaderStateObject;
_______________________________________________________________
_______________________________________________________________
public function AbstractLoadersContext()
_______________________________________________________________
{
_______________________________________________________________
addChild( _ldr = new Loader() );
_______________________________________________________________
_stateLoader = createState( this );
_______________________________________________________________
}
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-18. LoadersContext.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-19. ALoaderStateObject.as extends Object
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-20. EmptyLoaderStateObject.as extends ALoaderStateObject
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-21. OpeningConnectionStateObject.as extends ALoaderStateObject
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-22. LoadingStateObject.as extends ALoaderStateObject
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-23. LoadedStateObject.as extends ALoaderStateObject
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
19.Write an AbstractShape class and its subclasses, Square and Circle, so they can be drawn and cleared. Additionally, construct an AbstractCommand class that can execute and un-do said executed code. There are two possible solutions; write both.
Listing 9-24. IGraphics.as interface
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-25. AbstractShape.as constants WIDE and TALL are both 20 pixels. FILL_COLOR is yellow.
________________________________________________________________
protected const WIDE : int = 20;
________________________________________________________________
protected const TALL : int = 20;
________________________________________________________________
private const FILL_COLOR : uint = 0xfff000;
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-26. CircleShape.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-27. SquareShape.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-28. AbstractShapeCommand.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-29. ShapeCommandDraw.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-30. ShapeCommandUndo.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-31. AbstractShapeUndoCommand.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-32. ShapeCommandWithUndo.as
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
20.The Execute method accompanies which design pattern?
________________________________________________________________
21.Explain the advantage of the Abstract Factory pattern over the Factory Method pattern.
________________________________________________________________
________________________________________________________________
________________________________________________________________
22.In ActionScript 3.0, what are the three design patterns used in the EventSystem to carry out events of DisplayObjects?
________________________________________________________________
________________________________________________________________
________________________________________________________________
23.Three objects make up an image loader in an application: a loader, an image mask, and a description box. Using these three objects, the sequence must occur in the following order:
An image loads.
The mask transitions to reveal the image.
Text appears, giving a description.
Demonstrate how the Chain of Responsibility pattern can properly compliment the output of the following client code in Listing 9-33.
Listing 9-33. DocumentClass using the Chain of Responsibility pattern to accomplish its sequence
public function DocumentClass()
{
var img : AbstractView = new ImageView();
var mask : AbstractView = new MaskView();
img.addHandler( mask );
var tf : AbstractView = new TextFieldView();
mask.addHandler( tf );
tf.addHandler( IHandler( new NullHandler() ) );
}
//... [object ImageView] target hit;
//... [object MaskView] target hit;
//... [object TextFieldView] target hit;
//... [object NullHandler] target hit: end of Chain;
Listing 9-34. IHandler interface
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-35. AbstractView
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-36. ImageView.as loads the following image: www.spilled-milk.com/000.jpg.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
ldr.load( new URLRequest( "http://www.spilled-milk.com/000.jpg" ) );
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-37. MaskView
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Listing 9-38. DescriptionView
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
24.What pattern decouples multiple subsystems from client messaging by funneling those implementations into a simpler interface? The façade pattern
25.Choose the appropriate associations.
Model |
Composite pattern |
View |
Subject pattern |
Controller |
Observer pattern |
Strategy pattern |
Answers to Design Patterns Quiz
1. The common prefixes make, create, and get reflect which specific design pattern?
The Factory Method pattern
2. Primitive operations, which are prefixed with do, are used by which pattern?
The Template Method pattern
3. Separate the instantiation from its assembly to promote flexibility in Listing 9-1.
Listing 9-1. AbstractClass
public class AbstractClass extends Sprite
{
public function AbstractClass()
{
var someMovieClip : MovieClip = new ConcreteMovieClip();
someMovieClip.y = 25;
someMovieClip.x = 40;
someMovieClip.play();
addChild( someMovieClip );
}
}
Listing 9-2. AbstractClass
public class AbstractClass extends Sprite
________________________________________________________________
{
________________________________________________________________
public function AbstractClass ()
________________________________________________________________
{
________________________________________________________________
var mc : MovieClip = createMovie();
________________________________________________________________
mc.y = 25;
________________________________________________________________
mc.x = 40;
________________________________________________________________
mc.play();
________________________________________________________________
addChild( mc );
________________________________________________________________
}
________________________________________________________________
________________________________________________________________
protected function createMovie() : MovieClip
________________________________________________________________
{
________________________________________________________________
throw new IllegalOperationError( 'createMovie must be overridden' );
________________________________________________________________
return null;
________________________________________________________________
}
________________________________________________________________
}
________________________________________________________________
Listing 9-3. FactoryMethodClass.as
________________________________________________________________
public class FactoryMethodClass extends AbstractClass
________________________________________________________________
{
________________________________________________________________
override protected function createMovie() : MovieClip
________________________________________________________________
{
________________________________________________________________
return new ConcreteMovieClip();
________________________________________________________________
}
________________________________________________________________
}
________________________________________________________________
4. Design a decorator that enhances a bitmap with a scrollRect that can scroll to reveal concealed areas of the image using the mouse. The bitmap interface is IBitmap in Listing 9-4.
Listing 9-4. IBitmap
public interface IBitmap
{
function get bitmapData() : BitmapData;
function set bitmapData( value : BitmapData ) : void;
function get pixelSnapping() : String;
function set pixelSnapping( value : String ) : void;
function get smoothing() : Boolean;
function set smoothing( value : Boolean ) : void;
}
Listing 9-5. DecoratorAbstract.as
public class DecoratorAbstract extends Sprite implements IBitmap
___________________________________________________________________
{
___________________________________________________________________
protected var _decoratee : Bitmap;
___________________________________________________________________
___________________________________________________________________
public function DecoratorAbstract( decoratee : Bitmap ) : void
___________________________________________________________________
{
___________________________________________________________________
_decoratee = decoratee;
___________________________________________________________________
addChild( decoratee );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get bitmapData() : BitmapData
___________________________________________________________________
{
___________________________________________________________________
return _decoratee.bitmapData;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function set bitmapData( value : BitmapData ) : void
___________________________________________________________________
{
___________________________________________________________________
_decoratee.bitmapData = value;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get pixelSnapping() : String
___________________________________________________________________
{
___________________________________________________________________
return _decoratee.pixelSnapping;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function set pixelSnapping( value : String ) : void
___________________________________________________________________
{
___________________________________________________________________
_decoratee.pixelSnapping = value;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get smoothing() : Boolean
___________________________________________________________________
{
___________________________________________________________________
return _decoratee.smoothing;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function set smoothing( value : Boolean ) : void
___________________________________________________________________
{
___________________________________________________________________
_decoratee.smoothing = value;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-6. MouseScrollingDecorator.as
public class MouseScrollingDecorator extends DecoratorAbstract
___________________________________________________________________
{
___________________________________________________________________
protected var _viewport : Rectangle;
___________________________________________________________________
protected var _pixelsPerWide : int;
___________________________________________________________________
protected var _pixelsPerTall : int;
___________________________________________________________________
private var _rectTall : int = 400;
___________________________________________________________________
private var _rectWide : int = 400;
___________________________________________________________________
___________________________________________________________________
public function MouseScrollingDecorator( bitmap : Bitmap )
___________________________________________________________________
{
___________________________________________________________________
super( bitmap );
___________________________________________________________________
addEventListener( MouseEvent.MOUSE_MOVE , onMovement );
___________________________________________________________________
_decoratee.scrollRect = new Rectangle( 0 , 0 , _rectWide , _rectTall );
___________________________________________________________________
___________________________________________________________________
_pixelsPerWide = bitmap.width / _rectWide;
___________________________________________________________________
_pixelsPerTall = bitmap.height / _rectTall;
___________________________________________________________________
cacheAsBitmap = true;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function onMovement( event : MouseEvent ) : void
___________________________________________________________________
{
___________________________________________________________________
var localRect : Rectangle = this._decoratee.scrollRect;
___________________________________________________________________
localRect.x = event.localX * _pixelsPerWide;
___________________________________________________________________
localRect.y = event.localY;
___________________________________________________________________
_decoratee.scrollRect = localRect;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
5. Explain how you can subclass a method and provide it with an implementation without disturbing the algorithm in which the implementation is required.
The Template Method pattern offers the solution by marking the as final the method that defines the primitive operations that make up an algorithm. The primitive operations reveal hooks for which a subclass can provide implementations, while protecting the default implementation of the superclass.
6. Explain the benefits of an Iterator and an aggregate.
Using an iterator along with an aggregate conceals the logic that necessitates element retrieval from the aggregate, which would otherwise pollute a client with unnecessary details.
7. Define a component interface that favors transparency over safety in all of its components and leaves.
Listing 9-7. IComponent.as
public interface IComponent
___________________________________________________________________
{
___________________________________________________________________
function addComponent( cmpt : Component ) : void;
___________________________________________________________________
function removeComponent( cmpt : Component ) : void;
___________________________________________________________________
function operation() : void;
___________________________________________________________________
}
___________________________________________________________________
8. This pattern ensures state synchronization. The Observer pattern
9. Mushroomy Kingdom uses the console platform and its power to revisit stage 1-1 with dreamy textures. Table 9-1 lists the names that reference the linked images of the .fla.
Table 9-1. Stage 1-1 concretes revisited
New stone floor tile: StoneFlooring |
New money box tile: MoneyBox |
New brick tile: WhiteStone |
New pipe tile: IndustrialPlumbing |
Cloud: AlphaCloud |
Hill terrain: HillSide |
Using the AbstractMarioLevelDirector.as and the AbstractMarioEsqueLevelEditor.as code and the reference names from Table 9-1, write the implementations to populate this scene. The dimensions aren’t important.
Listing 9-8. AbstractMarioEsqueLevelEditor.as
public class AbstractMarioEsqueLevelEditor
{
private var _bitmapD : BitmapData;
private var _backgroundColor : uint;
private var _width : int;
private var _height : int;
private var _pt : Point;
private var _tile : Shape;
public function AbstractMarioEsqueLevelEditor()
{
_tile = new Shape();
_pt = new Point( 0 , 0 );
}
final public function createMap() : void
{
bitmap = doCreateMap();
}
final public function getLevel() : BitmapData
{
return _bitmapD;
}
final public function createStone( rect : Rectangle ) : void
{
addTile( doCreateStone() , rect );
}
final public function createSolidBrick( rect : Rectangle ) : void
{
addTile( doCreateSolidBrick() , rect );
}
final public function createBreakableBrick( rect : Rectangle ) : void
{
addTile( doCreateBreakableBrick() , rect );
}
final public function createMoneyBox( rect : Rectangle ) : void
{
addTile( doCreateMoneyBox() , rect );
}
final public function createCloud( rect : Rectangle ) : void
{
addTile( doCreateCloud() , rect );
}
final public function createHill( rect : Rectangle ) : void
{
addTile( doCreateHill() , rect );
}
final public function createBush( rect : Rectangle ) : void
{
addTile( doCreateBush() , rect );
}
final public function createCastle( rect : Rectangle ) : void
{
addTile( doCreateCastle() , rect );
}
final public function createPipe( rect : Rectangle ) : void
{
addTile( doCreatePipe() , rect );
}
final public function get width() : int
{
return _width;
}
final public function set width( width : int ) : void
{
_width = width;
}
final public function get height() : int
{
return _height;
}
final public function set height( height : int ) : void
{
_height = height;
}
final public function get backgroundColor() : uint
{
return _backgroundColor;
}
final public function set backgroundColor( backgroundColor : uint ) : void
{
_backgroundColor = backgroundColor;
}
final public function get bitmap() : BitmapData
{
return _bitmapD;
}
final public function set bitmap( bitmap : BitmapData ) : void
{
_bitmapD = bitmap;
}
protected function doCreateMap() : BitmapData
{
return new BitmapData( width , height , false , backgroundColor );
}
protected function doCreateStone() : DisplayObject
{
throw new IllegalOperationError( 'doCreateStone must be overridden' );
return null;
}
protected function doCreateSolidBrick() : DisplayObject
{
throw new IllegalOperationError( 'doCreateSolidBrick must be overridden' );
return null;
}
protected function doCreateBreakableBrick() : DisplayObject
{
throw new IllegalOperationError('doCreateBreakableBrick must be
overridden');
return null;
}
protected function doCreateMoneyBox() : DisplayObject
{
throw new IllegalOperationError( 'doCreateMoneyBox must be overridden' );
return null;
}
protected function doCreateCloud() : DisplayObject
{
throw new IllegalOperationError( 'doCreateCloud must be overridden' );
return null;
}
protected function doCreateHill() : DisplayObject
{
throw new IllegalOperationError( 'doCreateHill must be overridden' );
return null;
}
protected function doCreateBush() : DisplayObject
{
throw new IllegalOperationError( 'doCreateBush must be overridden' );
return null;
}
protected function doCreateCastle() : DisplayObject
{
throw new IllegalOperationError( 'doCreateCastle must be overridden' );
return null;
}
protected function doCreatePipe() : DisplayObject
{
throw new IllegalOperationError( 'doCreatePipe must be overridden' );
return null;
}
private function addTile( dO : DisplayObject , rect : Rectangle ) : void
{
var sprite : BitmapData = snapShot( dO );
_pt.x = rect.x;
_pt.y = rect.y;
if (rect.width > 0 || rect.height > 0)
{
sprite = tile( sprite , rect );
}
bitmap.copyPixels( sprite , sprite.rect , _pt );
}
private function snapShot( dO : DisplayObject ) : BitmapData
{
var snapshot : BitmapData = new BitmapData( dO.width, dO.height , true , 0 );
snapshot.draw( dO );
return snapshot;
}
private function tile( bmpd : BitmapData , rect : Rectangle ) : BitmapData
{
var _t : Shape = _tile;
var g : Graphics = _t.graphics;
g.clear();
g.beginBitmapFill( bmpd , null , true , false );
g.drawRect( 0 , 0 , rect.width , rect.height );
g.endFill();
return snapShot( _t );
}
}
Listing 9-9. QuizLevelEditor.as
public class QuizLevelEditor extends AbstractMarioEsqueLevelEditor
___________________________________________________________________
{
___________________________________________________________________
public function QuizLevelEditor()
___________________________________________________________________
{
___________________________________________________________________
super();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateMap() : BitmapData
___________________________________________________________________
{
___________________________________________________________________
return new BitmapData( width , height , false , backgroundColor );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateStone() : BitmapData
___________________________________________________________________
{
___________________________________________________________________
return new Stone();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateSolidBrick() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new StoneFlooring();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateBreakableBrick() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new WhiteStone();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateMoneyBox() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new MoneyBox();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateCloud() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new AlphaClouds();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateHill() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new HillSide();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateBush() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new Bush();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreateCastle() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new Castle();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doCreatePipe() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return new IndustrialPlumbing();
___________________________________________________________________
}
___________________________________________________________________
}
__________________________________________________________________
Listing 9-10. AbstractMarioLevelDirector.as
public class AbstractMarioLevelDirector
{
protected const _width : int = 400;
protected const _height : int = 300;
protected const _bgColor : uint = 0xacccff;
protected var _builder : AbstractMarioEsqueLevelEditor;
public function AbstractMarioLevelDirector( builder :
AbstractMarioEsqueLevelEditor )
{
_builder = builder;
}
public function getLevel() : BitmapData
{
return _builder.getLevel();
}
}
Listing 9-11. QuizLevelDirector.as
public class QuizLevelDirector extends AbstractMarioLevelDirector
___________________________________________________________________
{
___________________________________________________________________
private var rect : Rectangle = new Rectangle( 0 , 0 , 0 , 0 )
___________________________________________________________________
___________________________________________________________________
public function QuizLevelDirector( builder : AbstractMarioEsqueLevelEditor )
___________________________________________________________________
{
___________________________________________________________________
super( builder );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function getLevel() : BitmapData
___________________________________________________________________
{
___________________________________________________________________
_builder.width = _width;
___________________________________________________________________
_builder.height = _height;
___________________________________________________________________
_builder.backgroundColor = _bgColor;
___________________________________________________________________
_builder.createMap();
___________________________________________________________________
buildScenicTerrain();
___________________________________________________________________
buildScenicClouds();
___________________________________________________________________
buildScenicBricks();
___________________________________________________________________
buildFloor();
___________________________________________________________________
buildPipes();
___________________________________________________________________
buildMoneyBox();
___________________________________________________________________
___________________________________________________________________
return _builder.getLevel();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function buildMoneyBox() : void
___________________________________________________________________
{
___________________________________________________________________
assignRect( 210 , 40 );
___________________________________________________________________
_builder.createMoneyBox( rect );
___________________________________________________________________
assignRect( 80 , 130 );
___________________________________________________________________
_builder.createMoneyBox( rect );
___________________________________________________________________
assignRect( 180 , 130 );
___________________________________________________________________
_builder.createMoneyBox( rect );
___________________________________________________________________
assignRect( 230 , 130 );
___________________________________________________________________
_builder.createMoneyBox( rect );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function buildScenicBricks() : void
___________________________________________________________________
{
___________________________________________________________________
assignRect( 155 , 130 , 120 , 23 );
___________________________________________________________________
_builder.createBreakableBrick( rect );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function buildPipes() : void
___________________________________________________________________
{
___________________________________________________________________
assignRect( 330 , _height - 15 * 2 - 65 );
___________________________________________________________________
_builder.createPipe( rect );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function buildFloor() : void
___________________________________________________________________
{
___________________________________________________________________
assignRect( 0 , _height - 56 , _width , _height - 56 );
___________________________________________________________________
_builder.createSolidBrick( rect );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function buildScenicTerrain() : void
___________________________________________________________________
{
___________________________________________________________________
assignRect( 0 , 90 , _width , _height - 56 );
___________________________________________________________________
_builder.createHill( rect );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function buildScenicClouds() : void
___________________________________________________________________
{
___________________________________________________________________
assignRect( 0 , 0 , _width , 1 );
___________________________________________________________________
_builder.createCloud( rect );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function assignRect( x : int=0, y :
int=0, w : int=0, h : int=0 ) : void
___________________________________________________________________
{
___________________________________________________________________
rect.x = x;
___________________________________________________________________
rect.y = y;
___________________________________________________________________
rect.width = w;
___________________________________________________________________
rect.height = h;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
10.Explain why it’s unwise to use a Simple Singleton in an application.
Using a Simple Singleton in AS3 is a bad idea because it doesn’t give you the ability to extend the static instance. Additionally, the Simple Singleton tightly couples code with a static reference, which makes it difficult, or slower, to reuse code in the future.
11.The following code is from an unrevealed class.
Listing 9-12. UnRevealed class
...cont
public function makeFastFoodSandwich( menu_number : int ) : ValueMeal
{
switch(menu_number)
{
case 1:
return new DoubleStack();
break;
case 2 :
return new ChickenSandwich();
break;
case 3:
return new ChickenNuggets();
break;
case 4:
return new Frosty();
break;
}
}
...cont
The code in Listing 9-12 is a factory method. True
12.Twitter is the epitome of which design pattern? The Observer pattern
13.Having parallel hierarchies means you use fewer classes than when using orthogonal hierarchies. True
Quite the contrary. Any time you have parallel hierarchies, you multiply your system by the number of parallel hierarchies.
14.These three patterns can optionally intercept a request before passing it on.
Decorator pattern
Chain of Responsibility pattern
Adapter pattern
15.Show a loose composite that stops all MovieClips in the DisplayList.
//traverse(this.stage)
___________________________________________________________________
public function traverse( mc : DisplayObjectContainer ) : void
___________________________________________________________________
{
___________________________________________________________________
if (mc is MovieClip)
___________________________________________________________________
{
___________________________________________________________________
MovieClip( mc ).stop();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
if (mc.numChildren > 0)
___________________________________________________________________
{
___________________________________________________________________
for ( var i : int = 0; i < mc.numChildren; i++ )
___________________________________________________________________
{
___________________________________________________________________
var innards : DisplayObject = mc.getChildAt( i );
___________________________________________________________________
___________________________________________________________________
if ( innards is MovieClip)
___________________________________________________________________
{
___________________________________________________________________
traverse( MovieClip( innards ) );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
16.Re-create the display list from AS3 as a composite.
Listing 9-13. IComponent.as (DisplayObject) interface
public interface IComponent
___________________________________________________________________
{
___________________________________________________________________
function get parentComposite() : Component;
___________________________________________________________________
function set parentComposite( parentComposite : Component ) : void;
___________________________________________________________________
}
___________________________________________________________________
Listing 9-14. IComposite.as (DisplayObjectContainer) interface
public interface IComposite
___________________________________________________________________
{
___________________________________________________________________
function addChild( child : DisplayObject ) : DisplayObject;
___________________________________________________________________
function addChildAt( child : DisplayObject , index : int ) : DisplayObject;
___________________________________________________________________
function getChildAt( index : int ) : DisplayObject;
___________________________________________________________________
function getChildByName( name : String ) : DisplayObject;
___________________________________________________________________
function getChildIndex( child : DisplayObject ) : int;
___________________________________________________________________
function removeChild( child : DisplayObject ) : DisplayObject;
___________________________________________________________________
function removeChildAt( index : int ) : DisplayObject;
___________________________________________________________________
function setChildIndex( child : DisplayObject , index : int ) : void;
___________________________________________________________________
function swapChildren( child1 : DisplayObject , child2 : DisplayObject ) : void;
___________________________________________________________________
function swapChildrenAt( index1 : int , index2 : int ) : void;
___________________________________________________________________
}
___________________________________________________________________
Listing 9-15. Leaf.as (DisplayObject)
public class Leaf implements IComponent
{
}
17.What are the two most significant differences between the State pattern and the Strategy pattern?
1. The Strategy pattern requires the client to change behaviors, whereas the State pattern conceals the behaviors from the client for uniformity. (The state changes itself; the strategy is changed by the client).
2. The change in behaviors in the Strategy pattern reflects the needs of the client, whereas the change in behaviors in the State pattern reflects the change in the context’s state.
18.Suppose a loader uses the following states: Closed, OpeningConnection, Loading, and Loaded. Given the interface of ILoader shown in Listing 9-16, assemble a loader using only the State pattern, ensuring that the loader can load a new request at any given point in time, as well as be destroyed, without using any conditional statements.
Listing 9-16. Loader interface
public interface ILoader
{
function close();
function load( request : URLRequest , context : LoaderContext = null ) : void;
function loadBytes( bytes : ByteArray , context : LoaderContext = null ) : void;
function get content() : DisplayObject;
function get contentLoaderInfo() : LoaderInfo;
function get ldr() : Loader;
function dispose() : void;
}
Listing 9-17. AbstractLoadersContext.as
public class AbstractLoadersContext extends Sprite implements ILoader
___________________________________________________________________
{
___________________________________________________________________
private var _ldr : Loader;
___________________________________________________________________
protected var _stateLoader : ALoaderStateObject;
___________________________________________________________________
___________________________________________________________________
public function AbstractLoadersContext()
___________________________________________________________________
{
___________________________________________________________________
addChild( _ldr = new Loader() );
___________________________________________________________________
_stateLoader = createState( this );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function changeState( state : ALoaderStateObject ) : void
___________________________________________________________________
{
___________________________________________________________________
_stateLoader.dispose();
___________________________________________________________________
_stateLoader = state;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function close() : void
___________________________________________________________________
{
___________________________________________________________________
_stateLoader.close();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get content() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return _stateLoader.content;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get contentLoaderInfo() : LoaderInfo
___________________________________________________________________
{
___________________________________________________________________
return _stateLoader.contentLoaderInfo;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function load( request:URLRequest, context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
_stateLoader.load( request , context );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function loadBytes( bytes:ByteArray, context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
_stateLoader.loadBytes( bytes , context );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get ldr() : Loader
___________________________________________________________________
{
___________________________________________________________________
return _ldr;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function dispose() : void
___________________________________________________________________
{
___________________________________________________________________
_stateLoader.dispose();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
protected function createState( abstractLoadersContext :
AbstractLoadersContext) : ALoaderStateObject
___________________________________________________________________
{
___________________________________________________________________
throw new IllegalOperationError( 'createState must be overridden' )
___________________________________________________________________
return null;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-18. LoadersContext.as
public class LoadersContext extends AbstractLoadersContext
___________________________________________________________________
{
___________________________________________________________________
public function LoadersContext()
___________________________________________________________________
{
___________________________________________________________________
super();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function createState( abstractLoadersContext :
AbstractLoadersContext ) : ALoaderStateObject
___________________________________________________________________
{
___________________________________________________________________
return EmptyLoaderStateObject( abstractLoadersContext );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Explanation: LoadersContext uses a factory method, whereas the StateObjects don’t, because LoadersContext declares the initial StateObject. This is subject to change more than the StateObjects because the individual states typically have a particular successor.
Listing 9-19. ALoaderStateObject.as extends Object
public class ALoaderStateObject extends Object
___________________________________________________________________
{
___________________________________________________________________
protected var _ldrContext : LoadersContext;
___________________________________________________________________
protected var _ldr : Loader;
___________________________________________________________________
___________________________________________________________________
public function ALoaderStateObject( context : LoadersContext )
___________________________________________________________________
{
___________________________________________________________________
_ldrContext = context;
___________________________________________________________________
_ldr = context.ldr;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function close() : void
___________________________________________________________________
{
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get content() : DisplayObject
___________________________________________________________________
{
___________________________________________________________________
return null;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get contentLoaderInfo() : LoaderInfo
___________________________________________________________________
{
___________________________________________________________________
return null;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function load( request:URLRequest , context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function loadBytes( bytes:ByteArray, context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function unload() : void
___________________________________________________________________
{
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function unloadAndStop( gc : Boolean = true ) : void
___________________________________________________________________
{
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function get ldr() : Loader
___________________________________________________________________
{
___________________________________________________________________
return _ldr;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function set ldr( ldr : Loader ) : void
___________________________________________________________________
{
___________________________________________________________________
_ldr = ldr;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function dispose() : void
___________________________________________________________________
{
___________________________________________________________________
throw new IllegalOperationError( 'dispose must be overridden' );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-20. EmptyLoaderStateObject.as extends ALoaderStateObject
public class EmptyLoaderStateObject extends ALoaderStateObject
___________________________________________________________________
{
___________________________________________________________________
public function EmptyLoaderStateObject( context : LoadersContext )
___________________________________________________________________
{
___________________________________________________________________
super( context );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function get contentLoaderInfo() : LoaderInfo
___________________________________________________________________
{
___________________________________________________________________
return _ldr.loaderInfo;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function load( request:URLRequest , context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
_ldr.load( request , context );
___________________________________________________________________
_ldrContext.changeState( new OpeningConnectionStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function loadBytes( bytes:ByteArray , context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
_ldr.loadBytes( bytes , context );
___________________________________________________________________
_ldrContext.changeState( new OpeningConnectionStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function dispose() : void
___________________________________________________________________
{
___________________________________________________________________
_ldr = null;
___________________________________________________________________
_ldrContext = null;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-21. OpeningConnectionStateObject.as extends ALoaderStateObject
public class OpeningConnectionStateObject extends ALoaderStateObject
___________________________________________________________________
{
___________________________________________________________________
public function OpeningConnectionStateObject( context : LoadersContext )
___________________________________________________________________
{
___________________________________________________________________
super( context );
___________________________________________________________________
_ldr.contentLoaderInfo.addEventListener( Event.OPEN , onConnectionOpen );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function onConnectionOpen( event : Event ) : void
___________________________________________________________________
{
___________________________________________________________________
_ldrContext.changeState( new LoadingStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function get contentLoaderInfo() : LoaderInfo
___________________________________________________________________
{
___________________________________________________________________
return ldr.loaderInfo
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function dispose() : void
___________________________________________________________________
{
___________________________________________________________________
_ldr.contentLoaderInfo.removeEventListener( Event.OPEN , onConnectionOpen );
___________________________________________________________________
_ldr = null;
___________________________________________________________________
_ldrContext = null;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-22. LoadingStateObject.as extends ALoaderStateObject
public class LoadingStateObject extends ALoaderStateObject
___________________________________________________________________
{
___________________________________________________________________
public function LoadingStateObject( context : LoadersContext )
___________________________________________________________________
{
___________________________________________________________________
super( context );
___________________________________________________________________
_ldr.contentLoaderInfo.addEventListener( Event.COMPLETE , onComplete );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function onComplete( event : Event ) : void
___________________________________________________________________
{
___________________________________________________________________
_ldrContext.changeState( new LoadedStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function close() : void
___________________________________________________________________
{
___________________________________________________________________
_ldr.close();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function get contentLoaderInfo() : LoaderInfo
___________________________________________________________________
{
___________________________________________________________________
return ldr.loaderInfo;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function load( request:URLRequest, context:LoaderContext ) : void
___________________________________________________________________
{
___________________________________________________________________
close();
___________________________________________________________________
___________________________________________________________________
_ldr.load( request , context );
___________________________________________________________________
_ldrContext.changeState( new OpeningConnectionStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function loadBytes( bytes:ByteArray,context:LoaderContext ) : void
___________________________________________________________________
{
___________________________________________________________________
close();
___________________________________________________________________
_ldr.loadBytes( bytes , context );
___________________________________________________________________
_ldrContext.changeState( new OpeningConnectionStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function dispose() : void
___________________________________________________________________
{
___________________________________________________________________
_ldr.contentLoaderInfo.removeEventListener( Event.COMPLETE , onComplete );
___________________________________________________________________
_ldr = null;
___________________________________________________________________
_ldrContext = null;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-23. LoadedStateObject.as extends ALoaderStateObject
public class LoadedStateObject extends ALoaderStateObject
___________________________________________________________________
{
___________________________________________________________________
public function LoadedStateObject( context : LoadersContext )
___________________________________________________________________
{
___________________________________________________________________
super( context );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function close() : void
___________________________________________________________________
{
___________________________________________________________________
_ldr.unloadAndStop();
___________________________________________________________________
_ldr.unload();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function get contentLoaderInfo() : LoaderInfo
___________________________________________________________________
{
___________________________________________________________________
return _ldr.loaderInfo;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function load( request:URLRequest , context:LoaderContext = null ) : void
___________________________________________________________________
{
___________________________________________________________________
close();
___________________________________________________________________
_ldr.load( request , context );
___________________________________________________________________
_ldrContext.changeState( new OpeningConnectionStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function loadBytes( bytes:ByteArray , context:LoaderContext = null) : void
___________________________________________________________________
{
___________________________________________________________________
close();
___________________________________________________________________
_ldr.loadBytes( bytes , context );
___________________________________________________________________
_ldrContext.changeState( new OpeningConnectionStateObject( _ldrContext ) );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override public function dispose() : void
___________________________________________________________________
{
___________________________________________________________________
_ldr = null;
___________________________________________________________________
_ldrContext = null;
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
19.Write an AbstractShape class and its subclasses, Square and Circle, so they can be drawn and cleared. Additionally, construct an AbstractCommand class that can execute and unexecute code. There are two possible solutions; write both.
Listing 9-24. IGraphics.as interface
public interface IGraphics
___________________________________________________________________
{
___________________________________________________________________
function draw() : void;
___________________________________________________________________
function clear() : void;
___________________________________________________________________
function get parent() : DisplayObjectContainer;
___________________________________________________________________
}
___________________________________________________________________
Listing 9-25. AbstractShape.as constants WIDE and TALL are both 20 pixels. FILL_COLOR is yellow.
public class AbstractShape extends Shape implements IGraphics
___________________________________________________________________
{
___________________________________________________________________
protected const WIDE : int = 20;
___________________________________________________________________
protected const TALL : int = 20;
___________________________________________________________________
private const FILL_COLOR : uint = 0xfff000;
___________________________________________________________________
___________________________________________________________________
final public function draw() : void
___________________________________________________________________
{
___________________________________________________________________
addColor();
___________________________________________________________________
doDraw();
___________________________________________________________________
endFill();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
final public function clear() : void
___________________________________________________________________
{
___________________________________________________________________
this.graphics.clear();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
protected function doDraw() : void
___________________________________________________________________
{
___________________________________________________________________
throw new IllegalOperationError( 'doDraw must be overridden' );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function addColor() : void
___________________________________________________________________
{
___________________________________________________________________
this.graphics.beginFill( FILL_COLOR , 1 );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
private function endFill() : void
___________________________________________________________________
{
___________________________________________________________________
this.graphics.endFill();
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-26. CircleShape.as
public class CircleShape extends AbstractShape
___________________________________________________________________
{
___________________________________________________________________
override protected function doDraw() : void
___________________________________________________________________
{
___________________________________________________________________
var radius : Number = Math.sqrt( WIDE * WIDE + TALL * TALL );
___________________________________________________________________
this.graphics.drawCircle( 0 , 0 , radius );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-27. SquareShape.as
___________________________________________________________________
public class SquareShape extends AbstractShape
___________________________________________________________________
{
___________________________________________________________________
___________________________________________________________________
override protected function doDraw() : void
___________________________________________________________________
{
___________________________________________________________________
this.graphics.drawRect( 0 , 0 , WIDE , TALL );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-28. AbstractShapeCommand.as
public class AbstractShapeCommand
___________________________________________________________________
{
___________________________________________________________________
protected var _receiver : IGraphics;
___________________________________________________________________
___________________________________________________________________
public function AbstractShapeCommand( rcvr : IGraphics )
___________________________________________________________________
{
___________________________________________________________________
_receiver = rcvr;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
final public function execute() : void
___________________________________________________________________
{
___________________________________________________________________
doExecute();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
protected function doExecute() : void
___________________________________________________________________
{
___________________________________________________________________
throw new IllegalOperationError( 'doExecute must be overridden' );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-29. ShapeCommandDraw.as
public class ShapeCommandDraw extends AbstractShapeCommand
___________________________________________________________________
{
___________________________________________________________________
override protected function doExecute() : void
___________________________________________________________________
{
___________________________________________________________________
_receiver.draw();
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-30. ShapeCommandUndo.as
public class ShapeCommandUndo extends AbstractShapeCommand
___________________________________________________________________
{
___________________________________________________________________
override protected function doExecute() : void
___________________________________________________________________
{
___________________________________________________________________
_receiver.clear();
___________________________________________________________________
_receiver.parent.removeChild( DisplayObject( _receiver ) );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-31. AbstractShapeUndoCommand.as
public class AbstractShapeUndoCommand extends AbstractShapeCommand
___________________________________________________________________
{
___________________________________________________________________
public function undo() : void
___________________________________________________________________
{
___________________________________________________________________
doUndo();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
protected function doUndo() : void
___________________________________________________________________
{
___________________________________________________________________
throw new IllegalOperationError( 'doUndo must be overridden' );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-32. ShapeCommandWithUndo.as
public class ShapeCommandWithUndo extends AbstractShapeUndoCommand
___________________________________________________________________
{
___________________________________________________________________
override protected function doExecute() : void
___________________________________________________________________
{
___________________________________________________________________
_receiver.draw();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
override protected function doUndo() : void
___________________________________________________________________
{
___________________________________________________________________
_receiver.clear();
___________________________________________________________________
_receiver.parent.removeChild( DisplayObject( _receiver ) );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
20.The Execute method accompanies which design pattern?
The Command pattern
21.Explain the advantage of the Abstract Factory pattern over the Factory Method pattern
Unlike a factory method, an abstract factory is an object that manufactures products. This allows the abstract factory to be parameterized among other objects to which the manufacturing request can be delegated.
22.In ActionScript 3.0, what are the three design patterns used in the EventSystem to carry out events of DisplayObjects?
Composite pattern
Chain of Responsibility pattern
Observer pattern
23.Three objects make up an image loader in an application: a loader, an image mask, and a description box. Using these three objects, the sequence must occur in the following order:
a. An image loads.
b. The mask transitions to reveal the image.
c. Text appears, giving a description.
Demonstrate how the Chain of Responsibility pattern can properly compliment the output of the following client code in Listing 9-33.
Listing 9-33. DocumentClass using the Chain of Responsibility pattern to accomplish its sequence
public function DocumentClass()
{
var img : AbstractView = new ImageView();
var mask : AbstractView = new MaskView();
img.addHandler( mask );
var tf : AbstractView = new TextFieldView();
mask.addHandler( tf );
tf.addHandler( IHandler( new NullHandler() ) );
}
//... [object ImageView] target hit;
//... [object MaskView] target hit;
//... [object TextFieldView] target hit;
//... [object NullHandler] target hit: end of Chain;
Listing 9-34. IHandler interface
public interface IHandler
___________________________________________________________________
{
___________________________________________________________________
function addHandler( successor : IHandler ) : void;
___________________________________________________________________
___________________________________________________________________
function forward() : void;
___________________________________________________________________
}
___________________________________________________________________
Listing 9-35. AbstractView
public class AbstractView extends Sprite implements IHandler
___________________________________________________________________
{
___________________________________________________________________
protected var _handler : IHandler;
___________________________________________________________________
___________________________________________________________________
public function AbstractView()
___________________________________________________________________
{
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function addHandler( successor : IHandler ) : void
___________________________________________________________________
{
___________________________________________________________________
_handler = successor;
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
final public function forward() : void
___________________________________________________________________
{
___________________________________________________________________
doForward();
___________________________________________________________________
_handler.forward();
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
protected function doForward() : void
___________________________________________________________________
{
___________________________________________________________________
trace( this + ' target hit' );
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-36. ImageView.as loads the following image: www.spilled-milk.com/000.jpg.
public class ImageView extends AbstractView
___________________________________________________________________
{
___________________________________________________________________
protected var ldr : Loader
___________________________________________________________________
___________________________________________________________________
public function ImageView()
___________________________________________________________________
{
___________________________________________________________________
super();
___________________________________________________________________
ldr = new Loader();
___________________________________________________________________
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE , onImageLoad ) ;
___________________________________________________________________
ldr.load( new URLRequest( "http://www.spilled-milk.com/000.jpg" ) );
___________________________________________________________________
addChild( ldr );
___________________________________________________________________
}
___________________________________________________________________
___________________________________________________________________
public function onImageLoad( event : Event ) : void
___________________________________________________________________
{
___________________________________________________________________
forward();
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-37. MaskView
public class MaskView extends AbstractView
___________________________________________________________________
{
___________________________________________________________________
public function MaskView()
___________________________________________________________________
{
___________________________________________________________________
super();
___________________________________________________________________
}
___________________________________________________________________
}
___________________________________________________________________
Listing 9-38. TextFieldView
public class TextFieldView extends AbstractView
___________________________________________________________________
{
___________________________________________________________________
public function TextFieldView()
___________________________________________________________________
{
___________________________________________________________________
super();
___________________________________________________________________
}
}
___________________________________________________________________
24.What pattern decouples multiple subsystems from client messaging by funneling those implementations into a simpler interface? The façade pattern
25.Choose the appropriate associations: