10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

New Creator Classes: Integrating a Parameterized Factory Method<br />

As with the product classes, we can add a parameterized factory method to our<br />

application without modifying existing code. We won’t change the original<br />

PrintCenter abstract class and its derived classes, LowVolPrintCenter and<br />

HighVolPrintCenter. By letting these be, we don’t break the functionality of the old<br />

interface, and clients using it will continue to function. We will define a new abstract<br />

interface called NewPrintCenter with a parameterized factory method (Example 2-18).<br />

Example 2-18. NewPrintCenter.as<br />

package printcenters<br />

{<br />

import flash.errors.IllegalOperationError;<br />

}<br />

// ABSTRACT Class (should be subclassed and not instantiated)<br />

public class NewPrintCenter<br />

{<br />

}<br />

public function print(fn:String, cKind:uint):void<br />

{<br />

var printjob:IPrintjob = this.createPrintjob(cKind);<br />

printjob.start(fn);<br />

}<br />

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

protected function createPrintjob(cKind:uint):IPrintjob<br />

{<br />

throw new IllegalOperationError("Abstract method:<br />

must be overridden in a subclass");<br />

return null;<br />

}<br />

The factory method createPrintjob( ) takes a parameter of type uint that represents<br />

a kind of print job (color or black and white). In addition, the print( ) method also<br />

takes this as an additional parameter.<br />

We next create two concrete classes (Example 2-19 and Example 2-20) that extend<br />

the NewPrintCenter abstract class for low and high volume printing.<br />

Example 2-19. NewLowVolPrintCenter.as<br />

package printcenters<br />

{<br />

public class NewLowVolPrintCenter extends NewPrintCenter<br />

{<br />

public static const BW :uint = 0;<br />

public static const COLOR :uint = 1;<br />

override protected function createPrintjob(cKind:uint):IPrintjob<br />

Extended Example: Color Printing | 81

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

Saved successfully!

Ooh no, something went wrong!