{"id":649,"date":"2007-08-17T20:37:19","date_gmt":"2007-08-18T01:37:19","guid":{"rendered":"http:\/\/www.lonhosford.com\/lonblog\/?p=649"},"modified":"2015-07-31T18:25:52","modified_gmt":"2015-07-31T23:25:52","slug":"sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-2-of-2","status":"publish","type":"post","link":"https:\/\/www.lonhosford.com\/lonblog\/2007\/08\/17\/sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-2-of-2\/","title":{"rendered":"Factory Design Pattern Actionscript 3 Print Center &#8211; Sanders &#038; Cumaranatunge  &#8211; Part 2 of 2"},"content":{"rendered":"<p><strong>By Lon (Alonzo) Hosford<\/strong><\/p>\n<p>This is the expanded print center Factory design pattern from chapter 2 of <a href=\"http:\/\/www.amazon.com\/gp\/product\/0596528469?ie=UTF8&amp;tag=hosfordusa&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596528469\" target=\"_blank\">William Sanders and Chandima Cumaranatunge Actionscript 3.0 Design Patterns<\/a>.<\/p>\n<div style=\"font-size: 12px; font-weight: bold; background-color: #cccccc; text-align: center; float: left; margin-right: 5px; padding-top: 5px; padding-right: 5px; padding-left: 5px; border: 1px solid #000000;\"><a href=\"http:\/\/www.amazon.com\/gp\/product\/0596528469?ie=UTF8&amp;tag=hosfordusa&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596528469\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"Learn More\" src=\"http:\/\/lh3.ggpht.com\/_e5pwU0LJbN8\/TGiieXsRA6I\/AAAAAAAAFqk\/WQ78slkyGgk\/s800\/51UzqurElVL._SL160_.jpg\" alt=\"Actionscript 3 Design Patterns \" width=\"122\" height=\"160\" \/><\/a>Learn More<\/div>\n<p>This is an ActionScript project created in Flex Builder and updated to Flex Builder 4. <a href=\"https:\/\/www.lonhosford.com\/content\/flex\/design_patterns\/sanders_cumuaranatunge_as3_design_patterns\/Chapter02_Factory_Minimalist\/Chapter02_Factory_PrintCenters_Part_2.zip\">Download the example code<\/a>. 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 <code>Chapter02_Factory_PrintCenters<\/code>. For your convenience you can <a href=\"https:\/\/www.lonhosford.com\/content\/flex\/design_patterns\/sanders_cumuaranatunge_as3_design_patterns\/Chapter02_Factory_Minimalist\/Chapter02_Factory_PrintCenters_Part_2_Flash_CS4.zip\">download a Flash CS4 ready to go example<\/a>.<\/p>\n<p>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.<\/p>\n<p>The classes that from part 1 are not repeated. <a href=\"https:\/\/www.lonhosford.com\/lonblog\/?p=631\">Click here<\/a> to review them.<\/p>\n<p><strong>Application Class &#8211; Chapter02_Factory_PrintCenters<\/strong><br \/>\nThis is the client class. It repeats the client class discussed in <a href=\"https:\/\/www.lonhosford.com\/lonblog\/?p=631\">part 1<\/a> use or the <code>PrintCenter<\/code> classes. It also access the new <code>HighVolPrinterCenter2<\/code> and  <code>LowVolPrinterCenter2<\/code> classes on line 48 and 54 respectively. These are subclasses of the new <code>PrintCenter2<\/code>. These are new creator classes that take parameters to determine which <code>IPrintJob classes<\/code> to select.  The original PrintCenter subclasses <code>HighVolPrinterCenter2<\/code> and  <code>LowVolPrinterCenter2<\/code> still remain and still work.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/**\r\n * Demonstrates a more concrete example of decoupling the client, this file, from the products.\r\n * In this case the products are print jobs on various printers. The print jobs are not coupled\r\n * to the client. This example uses parameters for the factory choices.\r\n * &lt;p&gt;\r\n * This is part two of the example. Additional IPrintJob classes are added along with a second creator\r\n * classes that take parameters for the type of printing.\r\n * &lt;\/p&gt;\r\n * *\/\r\npackage\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\r\n\timport flash.display.Sprite;\r\n\r\n\timport printcenters.HighVolPrinterCenter;\r\n\timport printcenters.HighVolPrinterCenter2;\r\n\timport printcenters.LowVolPrinterCenter;\r\n\timport printcenters.LowVolPrinterCenter2;\r\n\timport printcenters.PrintCenter;\r\n\timport printcenters.PrintCenter2;\r\n\r\n\t\/\/ {SET STAGE SIZE AND SPEED HERE}\r\n\t&#x5B;SWF(width=500, height = 300, frameRate = 30)]\r\n\tpublic class Chapter02_Factory_PrintCenters extends Sprite\r\n\t{\r\n\t\tprivate var debugConsole:DebugConsole = DebugConsole.getInstance();\r\n\t\tpublic function Chapter02_Factory_PrintCenters()\r\n\t\t{\r\n\t\t\tstage.addChild(debugConsole);\r\n\t\t\tdebugConsole.width = stage.stageWidth;\r\n\t\t\tdebugConsole.height = stage.stageHeight;\r\n\r\n\t\t\tdebugConsole.write(&quot;Actionscript 3.0 Design Patterns&quot;);\r\n\t\t\tdebugConsole.write(&quot;William Sanders &amp; Chandima Cumaranatunge&quot;);\r\n\t\t\tdebugConsole.write(&quot;Chapter 2 Print Centers Example&quot;);\r\n\t\t\tdebugConsole.write(&quot;\\n&quot;);\r\n\r\n\t\t\tdebugConsole.write(&quot;\\nPrint LongThesis.doc to high volume printer.&quot;);\r\n\t\t\tvar pcHighVol:PrintCenter = new HighVolPrinterCenter();\r\n\t\t\tpcHighVol.print(&quot;LongThesis.doc&quot;);\r\n\r\n\t\t\tdebugConsole.write(&quot;\\nPrint ShortVita.doc to low volume printer.&quot;);\r\n\t\t\tvar pcLowVol:PrintCenter = new LowVolPrinterCenter();\r\n\t\t\tpcLowVol.print(&quot;ShortVita.doc&quot;);\r\n\r\n\t\t\tdebugConsole.write(&quot;\\nPrint LongThesis.doc to high volume BW printer.&quot;);\r\n\t\t\tvar pc2HighVol:PrintCenter2 = new HighVolPrinterCenter2();\r\n\t\t\tpc2HighVol.print(&quot;LongThesis.doc&quot;, HighVolPrinterCenter2.BW);\r\n\t\t\tdebugConsole.write(&quot;\\nPrint SalesReport.doc to high volume COLOR printer.&quot;);\r\n\t\t\tpc2HighVol.print(&quot;SalesReport.doc&quot;, HighVolPrinterCenter2.COLOR);\r\n\r\n\t\t\tdebugConsole.write(&quot;\\nPrint LongThesis.doc to low volume BW printer.&quot;);\r\n\t\t\tvar pc2LowVol:PrintCenter2 = new LowVolPrinterCenter2();\r\n\t\t\tpc2LowVol.print(&quot;LongThesis.doc&quot;, LowVolPrinterCenter2.BW);\r\n\t\t\tdebugConsole.write(&quot;\\nPrint SalesReport.doc to low volume COLOR printer.&quot;);\r\n\t\t\tpc2LowVol.print(&quot;SalesReport.doc&quot;, LowVolPrinterCenter2.COLOR);\r\n\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>PrintCenter2 Class<\/strong><br \/>\nThe main change over <code>PrintCenter<\/code> class is the <code>printType<\/code> parameter in the <code>print()<\/code> method on line 17 and the <code>createPrintJob()<\/code> factory method on line 27. Each <code>PrintCenter2<\/code> subclass uses the <code>printType<\/code> parameter to determine which <code>IPrintJob<\/code> class to create. This expands the capability of unlimited new<code> IPrintJob<\/code> classes for any single <code>PrintCenter2<\/code> creator class.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage printcenters\r\n{\r\n\timport flash.errors.IllegalOperationError;\r\n\t\/**\r\n\t * Handles file printing. Paramatizes the type of print job.\r\n\t * *\/\r\n\tpublic class PrintCenter2\r\n\t{\r\n\t\tpublic function PrintCenter2()\r\n\t\t{\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Simulate printing a file.\r\n\t\t * @param fileName Name of file to print.\r\n\t\t * @param printType Name of file to print.\r\n\t\t * *\/\r\n\t\tpublic function print(fileName:String, printType:uint):void\r\n\t\t{\r\n\t\t\tvar printjob:IPrintJob = this.createPrintJob(printType);\r\n\t\t\tprintjob.start(fileName);\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Creates the IPrintJob products.\r\n\t\t * @throws flash.errors.IllegalOperationError Must override in subclass.\r\n\t\t * @param printType Name of file to print.\r\n\t\t * *\/\r\n\t\tprotected function createPrintJob(printType:uint):IPrintJob\r\n\t\t{\r\n\t\t\tthrow new IllegalOperationError(&quot;PrintCenter2.createPrintJob() - override in subclass&quot;);\r\n\t\t\treturn null;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>HighVolPrintCenter2 Class<\/strong><br \/>\nThe key change over the <code>HighVolPrintCenter<\/code> class are the <code>BW<\/code> and <code>COLOR<\/code> constants on line 10 and 11 respectively. These are the <code>printType<\/code> parameter values for selecting the <code>IPrintJob<\/code> class to use.<\/p>\n<p>The createPrintJob() function on line 23 uses the code>BW<\/code> and <code>COLOR<\/code> constants to select the <code>WorkgroupPrintJob<\/code> and the new <code>ColorLaserPrintJob<\/code> product classes. Any other parameter throws an <code>Error<\/code>.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage printcenters\r\n{\r\n\timport com.lonhosford.util.debug.lite.Debugger;\r\n\t\/**\r\n\t * HighVolPrinterCenter creator class\r\n\t * *\/\r\n\tpublic class HighVolPrinterCenter2 extends PrintCenter2\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic static const BW:uint = 0;\r\n\t\tpublic static const COLOR:uint = 1;\r\n\t\tpublic function HighVolPrinterCenter2()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;HighVolPrinterCenter2() - This is a creator.&quot;)\r\n\t\t\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Create IPrintJob object.\r\n\t\t * @param printType Name of file to print.\r\n\t\t * @return ColorLaserPrintJob | WorkgroupPrintJob based on printType\r\n\t\t * @throws Error When printType is not matched.\r\n\t\t * *\/\r\n\t\toverride protected function createPrintJob(printType:uint):IPrintJob\r\n\t\t{\r\n\t\t\t\r\n\t\t\tif (printType == BW)\r\n\t\t\t{\r\n\t\t\t\tdebugger.write(&quot;HighVolPrinterCenter2.createPrintJob() - BW&quot;);\r\n\t\t\t\treturn new WorkgroupPrintJob();\r\n\t\t\t}\r\n\t\t\telse if (printType == COLOR)\r\n\t\t\t{\r\n\t\t\t\tdebugger.write(&quot;HighVolPrinterCenter2.createPrintJob() - COLOR&quot;);\r\n\t\t\t\treturn new ColorLaserPrintJob();\r\n\t\t\t\t\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tthrow new Error(&quot;HighVolPrinterCenter2.createPrintJob() - Invalid printer kind.&quot;);\r\n\t\t\t\treturn null;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>LowVolPrintCenter2 Class<\/strong><br \/>\nThis mimics the <code>HighVolPrintCenter2<\/code> class.  The <code>InkJetPrintJob<\/code> and the new <code>ColorInkjetPrintJob<\/code> product classes are selected by the <code>printerType <\/code>parameter supplied to the <code>createPrintJob(...)<\/code> function on line 23. Lines 10 and 11 contain valid values for the <code>printerType <\/code>parameter.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage printcenters\r\n{\r\n\timport com.lonhosford.util.debug.lite.Debugger;\r\n\t\/**\r\n\t * LowVolPrinterCenter creator class\r\n\t * *\/\r\n\tpublic class LowVolPrinterCenter2 extends PrintCenter2\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic static const BW:uint = 0;\r\n\t\tpublic static const COLOR:uint = 1;\r\n\t\tpublic function LowVolPrinterCenter2()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;LowVolPrinterCenter2() - This is a creator.&quot;)\r\n\t\t\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Create IPrintJob object.\r\n\t\t * @param printType Name of file to print.\r\n\t\t * @return InkJetPrintJob | ColorInkjetPrintJob based on printType\r\n\t\t * @throws Error When printType is not matched.\r\n\t\t * *\/\r\n\t\toverride protected function createPrintJob(printType:uint):IPrintJob\r\n\t\t{\r\n\t\t\t\r\n\t\t\tif (printType == BW)\r\n\t\t\t{\r\n\t\t\t\tdebugger.write(&quot;LowVolPrinterCenter2.createPrintJob() - BW&quot;);\r\n\t\t\t\treturn new InkJetPrintJob();\r\n\t\t\t}\r\n\t\t\telse if (printType == COLOR)\r\n\t\t\t{\r\n\t\t\t\tdebugger.write(&quot;LowVolPrinterCenter2.createPrintJob() - COLOR&quot;);\r\n\t\t\t\treturn new ColorInkjetPrintJob();\r\n\t\t\t\t\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tthrow new Error(&quot;LowVolPrinterCenter2.createPrintJob() - Invalid low volume print type.&quot;);\r\n\t\t\t\treturn null;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\nThere are some new IPrintJob classes introduced. They simulate newer types of products available to the creator classes.<\/p>\n<p><strong>ColorLaserPrintJob Class<\/strong><br \/>\nNothing unusual here. Just another <code>IPrintJob<\/code> class.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage printcenters\r\n{\r\n\timport com.lonhosford.util.debug.lite.Debugger;\r\n\t\/**\r\n\t * ColorLaserPrintJob product class\r\n\t * *\/\r\n\tinternal class ColorLaserPrintJob implements IPrintJob\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function ColorLaserPrintJob()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;ColorLaserPrintJob()&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Simulate starting an ColorInkjetPrintJob\r\n\t\t * @param fileName Name of file to print.\r\n\t\t * *\/\t\t\r\n\t\tpublic function start(fileName:String):void\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;ColorLaserPrintJob.start() - fileName:&quot; + fileName);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p><strong>ColorInkjetPrintJob Class<\/strong><br \/>\nAnother <code>IPrintJob<\/code> class. They can be pounded out. Anyone want to make a dot matrix print job?<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage printcenters\r\n{\r\n\timport com.lonhosford.util.debug.lite.Debugger;\r\n\t\/**\r\n\t * ColorInkjetPrintJob product class\r\n\t * *\/\r\n\tinternal class ColorInkjetPrintJob implements IPrintJob\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function ColorInkjetPrintJob()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;ColorInkjetPrintJob()&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Simulate starting an ColorInkjetPrintJob\r\n\t\t * @param fileName Name of file to print.\r\n\t\t * *\/\t\t\r\n\t\tpublic function start(fileName:String):void\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;ColorInkjetPrintJob.start() - fileName:&quot; + fileName);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#appId=105467682877384&amp;xfbml=1\"><\/script><fb:like href=\"https:\/\/www.lonhosford.com\/lonblog\/2007\/08\/17\/sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-2-of-2\/\" send=\"true\" width=\"450\" show_faces=\"true\" font=\"\"><\/fb:like><\/p>\n<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#xfbml=1\"><\/script><fb:comments href=\"https:\/\/www.lonhosford.com\/lonblog\/2007\/08\/17\/sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-2-of-2\/\" num_posts=\"2\" width=\"500\"><\/fb:comments><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[19,12,39,36,9,31,38],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-general","tag-actionscript","tag-air-adobe-integrated-runtime","tag-cumaranatunge","tag-factory","tag-flex","tag-gaming","tag-sanders"],"_links":{"self":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":22,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":3711,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/649\/revisions\/3711"}],"wp:attachment":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}