{"id":631,"date":"2007-08-15T20:03:07","date_gmt":"2007-08-16T01:03:07","guid":{"rendered":"http:\/\/www.lonhosford.com\/lonblog\/?p=631"},"modified":"2015-07-31T18:25:52","modified_gmt":"2015-07-31T23:25:52","slug":"sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-1-of-2","status":"publish","type":"post","link":"https:\/\/www.lonhosford.com\/lonblog\/2007\/08\/15\/sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-1-of-2\/","title":{"rendered":"Factory Design Pattern Print Center Actionscript 3 &#8211; Sanders &#038; Cumaranatunge  &#8211; Part 1 of 2"},"content":{"rendered":"<p><strong>By Lon (Alonzo) Hosford<\/strong><\/p>\n<p>This is the 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_1.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_1_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><strong>Application Class &#8211; Chapter02_Factory_PrintCenters<\/strong><br \/>\nThis is the client class. The class instantiates <code>HighVolPrinterCenter<\/code> and <code>LowVolPrinterCenter<\/code> classes and calls their <code>print()<\/code> method inherited from the <code>PrintCenter<\/code> class. The basic idea is the same as the <a href=\"https:\/\/www.lonhosford.com\/lonblog\/?p=562\" >minimalist example<\/a> to uncouple the product classes from the client<code>InkJetPrintJob<\/code> and <code>WorkgroupPrintJob<\/code>.<\/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. Clients interface with creator classes representing a type of print center. \r\n * The product classes doing the work are created by the print center creator classes.\r\n * &lt;p&gt;\r\n * This is part one of the example. \r\n * &lt;\/p&gt;\r\n * *\/\r\npackage\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\t\r\n\timport flash.display.Sprite;\r\n\t\r\n\timport printcenters.HighVolPrinterCenter;\r\n\timport printcenters.LowVolPrinterCenter;\r\n\timport printcenters.PrintCenter;\r\n\t\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\t\t\t\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 - Part 1&quot;);\r\n\t\t\tdebugConsole.write(&quot;\\n&quot;);\r\n\t\t\t\r\n\t\t\t\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\t\t\t\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\t\t\t\r\n\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>PrintCenter Class<\/strong><br \/>\nThis class provides a &#8220;factory method&#8221; interface to each of its subclasses <code>LowVolPrintCenter<\/code> and <code>HighVolPrintCenter<\/code>. The line 25 shows the <code>createPrintJob()()<\/code> method. The factory method cannot be called from a client class. The method throws an <code>IllegalOperationError<\/code> to prevent that coding option.<\/p>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\nThe <code>createPrintJob()()<\/code> method returns a <code>IPrintJob<\/code> interface. Each Creator subclass will return its own product all having a <code>createPrintJob()()<\/code> method defined by the <code>IPrintJob<\/code> interface discussed later in this post. This <code>PrintCenter<\/code> class then uses the product class <code>start()<\/code> method on line 19.<\/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.\r\n\t * *\/\r\n\tpublic class PrintCenter\r\n\t{\r\n\t\tpublic function PrintCenter()\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 * *\/\t\t\r\n\t\tpublic function print(fileName:String):void\r\n\t\t{\r\n\t\t\tvar printjob:IPrintJob = this.createPrintJob();\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 * *\/\t\t\r\n\t\tprotected function createPrintJob():IPrintJob\r\n\t\t{\r\n\t\t\tthrow new IllegalOperationError(&quot;PrintCenter.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>LowVolPrinterCenter Class<\/strong><br \/>\nThis is a class the client classes use. The <code>PrintCenter<\/code> super class contains the <code>print()<\/code> method clients use to print a document. The <code>LowVolPrinterCenter<\/code> then creates the correct <code>IPrintJob<\/code> class to do the work. In this case it is the <code>InkJetPrintJob<\/code> class. If programming requires using another IPrintJob class, the change does not impact the <code>print()<\/code> method interface client classes use.<\/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 LowVolPrinterCenter extends PrintCenter\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function LowVolPrinterCenter()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;LowVolPrinterCenter() - This is a creator.&quot;)\r\n\t\t\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Create InkJetPrintJob object.\r\n\t\t * @return InkJetPrintJob\r\n\t\t * *\/\r\n\t\toverride protected function createPrintJob():IPrintJob\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;LowVolPrinterCenter.createPrintJob()&quot;);\r\n\t\t\treturn new InkJetPrintJob();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>HighVolPrinterCenter Class<\/strong><br \/>\nThis is a second subclass to the <code>PrintCenter<\/code> class. It uses the <code>IPrintJob<\/code> class <code>WorkgroupPrintJob<\/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 HighVolPrinterCenter extends PrintCenter\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function HighVolPrinterCenter()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;HighVolPrinterCenter() - This is a creator.&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Create WorkgroupPrintJob object.\r\n\t\t * @return WorkgroupPrintJob\r\n\t\t * *\/\r\n\t\toverride protected function createPrintJob():IPrintJob\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;HighVolPrinterCenter.createPrintJob()&quot;);\r\n\t\t\treturn new WorkgroupPrintJob();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\nNow there are two PrintCenter classes available to client programs. Both use the <code>print()<\/code> method to print documents. Next we look at the <code>IPrintJob<\/code> classes starting with the interface.<\/p>\n<p><strong>IPrintJob Interface<\/strong><br \/>\nThis interface defines one method <code>start()<\/code> for all <code>IPrintJob<\/code> classes to implement.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage printcenters\r\n{\r\n\t\/**\r\n\t * Sets the interface for print job product classes\r\n\t * *\/\r\n\tpublic interface IPrintJob\r\n\t{\r\n\t\t\/**\r\n\t\t * @param fileName Name of file to print.\r\n\t\t * *\/\t\t\r\n\t\tfunction start(fileName:String):void;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>InkJetPrintJob Class<\/strong><br \/>\nThis class implements the <code>IPrintJob<\/code> interface and includes the required <code>start()<\/code> method on line 18.<\/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 * InkJetPrintJob product class\r\n\t * *\/\r\n\tinternal class InkJetPrintJob implements IPrintJob\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function InkJetPrintJob()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;InkJetPrintJob()&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Simulate starting an InkJetPrintJob\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;InkJetPrintJob.start() - fileName:&quot; + fileName);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>WorkgroupPrintJob Class<\/strong><br \/>\nLike the <code>InkJetPrintJob<\/code> class this class implements the <code>IPrintJob<\/code> interface and includes the required <code>start()<\/code> method on line 18.<\/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 * WorkgroupPrintJob product class\r\n\t * *\/\r\n\tinternal class WorkgroupPrintJob implements IPrintJob\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function WorkgroupPrintJob()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;WorkgroupPrintJob()&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Simulate starting an WorkgroupPrintJob\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;WorkgroupPrintJob.start() - fileName:&quot; + fileName);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Adding to the PrintCenter class<\/strong><br \/>\nIt is relatively easy to add another <code>PrintCenter<\/code> class. Here is the <code>FancyPrintCenter<\/code> class that creates the <code>IProduct<\/code> class <code>MultifunctionPrintJob<\/code>. Create the classes in the and see if you can add them to the application <code>Chapter02_Factory_PrintCenters<\/code> class.<\/p>\n<p><strong>FancyPrinterCenter  class<\/strong><\/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 * FancyPrinterCenter creator class\r\n\t * *\/\r\n\tpublic class FancyPrinterCenter extends PrintCenter\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function FancyPrinterCenter()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;FancyPrinterCenter() - This is a creator.&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Create IPrintJob object.\r\n\t\t * @return MultiFunctionPrintJob\r\n\t\t * *\/\r\n\t\toverride protected function createPrintJob():IPrintJob\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;FancyPrinterCenter.createPrintJob()&quot;);\r\n\t\t\treturn new MultiFunctionPrintJob();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>MultiFunctionPrintJob class<\/strong><\/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 * MultiFunctionPrintJob product class\r\n\t * *\/\r\n\tinternal class MultiFunctionPrintJob implements IPrintJob\r\n\t{\r\n\t\tprivate var debugger:Debugger = Debugger.getInstance();\r\n\t\tpublic function MultiFunctionPrintJob()\r\n\t\t{\r\n\t\t\tdebugger.write(&quot;MultiFunctionPrintJob()&quot;)\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Simulate starting an MultiFunctionPrintJob\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;MultiFunctionPrintJob.start() - fileName:&quot; + fileName);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/www.lonhosford.com\/lonblog\/?p=649\">Part 2<\/a> introduces parameters for selection product classes. <\/p>\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\/15\/sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-1-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=\"hhttps:\/\/www.lonhosford.com\/lonblog\/2007\/08\/15\/sanders-cumaranatunge-print-center-actionscript-3-factory-design-pattern-part-1-of-2\/\" num_posts=\"2\" width=\"500\"><\/fb:comments><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By Lon (Alonzo) Hosford This is the 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 [&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,37,36,31,38],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-general","tag-actionscript","tag-air-adobe-integrated-runtime","tag-cumaranatunge","tag-design-patterns","tag-factory","tag-gaming","tag-sanders"],"_links":{"self":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/631","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=631"}],"version-history":[{"count":24,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":3713,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/631\/revisions\/3713"}],"wp:attachment":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}