By Lon (Alonzo) Hosford
This is the expanded print center Factory design pattern from chapter 2 of William Sanders and Chandima Cumaranatunge Actionscript 3.0 Design Patterns.
Learn More
This is an ActionScript project created in Flex Builder and updated to Flex Builder 4. Download the example code. You can build this with the free Flex SDK by using the code in the src folder. Same for Flash CS3 and CS4. You need to create a Flash Document in the src folder and set the document class to Chapter02_Factory_PrintCenters
. For your convenience you can download a Flash CS4 ready to go example.
This includes a basic Actionscript debugger console to display tracing statements on stage. Each class sends messages to the console to show their methods working. These messages help you follow the relationships in the Factory design pattern.
The classes that from part 1 are not repeated. Click here to review them.
Application Class – Chapter02_Factory_PrintCenters
This is the client class. It repeats the client class discussed in part 1 use or the PrintCenter
classes. It also access the new HighVolPrinterCenter2
and LowVolPrinterCenter2
classes on line 48 and 54 respectively. These are subclasses of the new PrintCenter2
. These are new creator classes that take parameters to determine which IPrintJob classes
to select. The original PrintCenter subclasses HighVolPrinterCenter2
and LowVolPrinterCenter2
still remain and still work.
/**
* Demonstrates a more concrete example of decoupling the client, this file, from the products.
* In this case the products are print jobs on various printers. The print jobs are not coupled
* to the client. This example uses parameters for the factory choices.
* <p>
* This is part two of the example. Additional IPrintJob classes are added along with a second creator
* classes that take parameters for the type of printing.
* </p>
* */
package
{
import com.lonhosford.util.debug.lite.DebugConsole;
import flash.display.Sprite;
import printcenters.HighVolPrinterCenter;
import printcenters.HighVolPrinterCenter2;
import printcenters.LowVolPrinterCenter;
import printcenters.LowVolPrinterCenter2;
import printcenters.PrintCenter;
import printcenters.PrintCenter2;
// {SET STAGE SIZE AND SPEED HERE}
[SWF(width=500, height = 300, frameRate = 30)]
public class Chapter02_Factory_PrintCenters extends Sprite
{
private var debugConsole:DebugConsole = DebugConsole.getInstance();
public function Chapter02_Factory_PrintCenters()
{
stage.addChild(debugConsole);
debugConsole.width = stage.stageWidth;
debugConsole.height = stage.stageHeight;
debugConsole.write("Actionscript 3.0 Design Patterns");
debugConsole.write("William Sanders & Chandima Cumaranatunge");
debugConsole.write("Chapter 2 Print Centers Example");
debugConsole.write("\n");
debugConsole.write("\nPrint LongThesis.doc to high volume printer.");
var pcHighVol:PrintCenter = new HighVolPrinterCenter();
pcHighVol.print("LongThesis.doc");
debugConsole.write("\nPrint ShortVita.doc to low volume printer.");
var pcLowVol:PrintCenter = new LowVolPrinterCenter();
pcLowVol.print("ShortVita.doc");
debugConsole.write("\nPrint LongThesis.doc to high volume BW printer.");
var pc2HighVol:PrintCenter2 = new HighVolPrinterCenter2();
pc2HighVol.print("LongThesis.doc", HighVolPrinterCenter2.BW);
debugConsole.write("\nPrint SalesReport.doc to high volume COLOR printer.");
pc2HighVol.print("SalesReport.doc", HighVolPrinterCenter2.COLOR);
debugConsole.write("\nPrint LongThesis.doc to low volume BW printer.");
var pc2LowVol:PrintCenter2 = new LowVolPrinterCenter2();
pc2LowVol.print("LongThesis.doc", LowVolPrinterCenter2.BW);
debugConsole.write("\nPrint SalesReport.doc to low volume COLOR printer.");
pc2LowVol.print("SalesReport.doc", LowVolPrinterCenter2.COLOR);
}
}
}
PrintCenter2 Class
The main change over PrintCenter
class is the printType
parameter in the print()
method on line 17 and the createPrintJob()
factory method on line 27. Each PrintCenter2
subclass uses the printType
parameter to determine which IPrintJob
class to create. This expands the capability of unlimited new IPrintJob
classes for any single PrintCenter2
creator class.
package printcenters
{
import flash.errors.IllegalOperationError;
/**
* Handles file printing. Paramatizes the type of print job.
* */
public class PrintCenter2
{
public function PrintCenter2()
{
}
/**
* Simulate printing a file.
* @param fileName Name of file to print.
* @param printType Name of file to print.
* */
public function print(fileName:String, printType:uint):void
{
var printjob:IPrintJob = this.createPrintJob(printType);
printjob.start(fileName);
}
/**
* Creates the IPrintJob products.
* @throws flash.errors.IllegalOperationError Must override in subclass.
* @param printType Name of file to print.
* */
protected function createPrintJob(printType:uint):IPrintJob
{
throw new IllegalOperationError("PrintCenter2.createPrintJob() - override in subclass");
return null;
}
}
}
HighVolPrintCenter2 Class
The key change over the HighVolPrintCenter
class are the BW
and COLOR
constants on line 10 and 11 respectively. These are the printType
parameter values for selecting the IPrintJob
class to use.
The createPrintJob() function on line 23 uses the code>BW and COLOR
constants to select the WorkgroupPrintJob
and the new ColorLaserPrintJob
product classes. Any other parameter throws an Error
.
package printcenters
{
import com.lonhosford.util.debug.lite.Debugger;
/**
* HighVolPrinterCenter creator class
* */
public class HighVolPrinterCenter2 extends PrintCenter2
{
private var debugger:Debugger = Debugger.getInstance();
public static const BW:uint = 0;
public static const COLOR:uint = 1;
public function HighVolPrinterCenter2()
{
debugger.write("HighVolPrinterCenter2() - This is a creator.")
}
/**
* Create IPrintJob object.
* @param printType Name of file to print.
* @return ColorLaserPrintJob | WorkgroupPrintJob based on printType
* @throws Error When printType is not matched.
* */
override protected function createPrintJob(printType:uint):IPrintJob
{
if (printType == BW)
{
debugger.write("HighVolPrinterCenter2.createPrintJob() - BW");
return new WorkgroupPrintJob();
}
else if (printType == COLOR)
{
debugger.write("HighVolPrinterCenter2.createPrintJob() - COLOR");
return new ColorLaserPrintJob();
}
else
{
throw new Error("HighVolPrinterCenter2.createPrintJob() - Invalid printer kind.");
return null;
}
}
}
}
LowVolPrintCenter2 Class
This mimics the HighVolPrintCenter2
class. The InkJetPrintJob
and the new ColorInkjetPrintJob
product classes are selected by the printerType
parameter supplied to the createPrintJob(...)
function on line 23. Lines 10 and 11 contain valid values for the printerType
parameter.
package printcenters
{
import com.lonhosford.util.debug.lite.Debugger;
/**
* LowVolPrinterCenter creator class
* */
public class LowVolPrinterCenter2 extends PrintCenter2
{
private var debugger:Debugger = Debugger.getInstance();
public static const BW:uint = 0;
public static const COLOR:uint = 1;
public function LowVolPrinterCenter2()
{
debugger.write("LowVolPrinterCenter2() - This is a creator.")
}
/**
* Create IPrintJob object.
* @param printType Name of file to print.
* @return InkJetPrintJob | ColorInkjetPrintJob based on printType
* @throws Error When printType is not matched.
* */
override protected function createPrintJob(printType:uint):IPrintJob
{
if (printType == BW)
{
debugger.write("LowVolPrinterCenter2.createPrintJob() - BW");
return new InkJetPrintJob();
}
else if (printType == COLOR)
{
debugger.write("LowVolPrinterCenter2.createPrintJob() - COLOR");
return new ColorInkjetPrintJob();
}
else
{
throw new Error("LowVolPrinterCenter2.createPrintJob() - Invalid low volume print type.");
return null;
}
}
}
}
[ad name=”Google Adsense”]
There are some new IPrintJob classes introduced. They simulate newer types of products available to the creator classes.
ColorLaserPrintJob Class
Nothing unusual here. Just another IPrintJob
class.
package printcenters
{
import com.lonhosford.util.debug.lite.Debugger;
/**
* ColorLaserPrintJob product class
* */
internal class ColorLaserPrintJob implements IPrintJob
{
private var debugger:Debugger = Debugger.getInstance();
public function ColorLaserPrintJob()
{
debugger.write("ColorLaserPrintJob()")
}
/**
* Simulate starting an ColorInkjetPrintJob
* @param fileName Name of file to print.
* */
public function start(fileName:String):void
{
debugger.write("ColorLaserPrintJob.start() - fileName:" + fileName);
}
}
}
ColorInkjetPrintJob Class
Another IPrintJob
class. They can be pounded out. Anyone want to make a dot matrix print job?
package printcenters
{
import com.lonhosford.util.debug.lite.Debugger;
/**
* ColorInkjetPrintJob product class
* */
internal class ColorInkjetPrintJob implements IPrintJob
{
private var debugger:Debugger = Debugger.getInstance();
public function ColorInkjetPrintJob()
{
debugger.write("ColorInkjetPrintJob()")
}
/**
* Simulate starting an ColorInkjetPrintJob
* @param fileName Name of file to print.
* */
public function start(fileName:String):void
{
debugger.write("ColorInkjetPrintJob.start() - fileName:" + fileName);
}
}
}