ActionScript 3.0 Design Patterns

(Chris Devlin) #1

82 | Chapter 2: Factory Method Pattern


We have created a new hierarchy of related classes for the new print centers, with


parameterized factory methods. The abstract class isNewPrintCenter,and its derived


subclasses areNewLowVolPrintCenterandNewHighVolPrintCenter. Note the public


static constants to identify the different kinds of print jobs. These constants are pub-


licly accessible and should be used as the parameters passed to theprint( ) method.


{

if (cKind == BW)
{
trace("Creating new printjob for the b/w inkjet printer");
return new InkjetPrintjob( );
} else if (cKind == COLOR) {
trace("Creating new printjob for the color inkjet printer");
return new ColorInkjetPrintjob( );
} else {
throw new Error("Invalid low volume print job");
return null;
}
}
}
}


Example 2-20. NewHighVolPrintCenter.as


package printcenters
{
public class NewHighVolPrintCenter extends NewPrintCenter
{
public static const BW :uint = 0;
public static const COLOR :uint = 1;


override protected function createPrintjob(cKind:uint):IPrintjob
{
if (cKind == BW)
{
trace("Creating new printjob for the b/w workgroup printer");
return new WorkgroupPrintjob( );
} else if (cKind == COLOR) {
trace("Creating new printjob for the color laser printer");
return new ColorLaserPrintjob( );
} else {
throw new Error("Invalid high volume print job");
return null;
}
}
}
}


Example 2-19. NewLowVolPrintCenter.as

Free download pdf