{"id":562,"date":"2007-08-15T21:13:41","date_gmt":"2007-08-16T02:13:41","guid":{"rendered":"http:\/\/www.lonhosford.com\/lonblog\/?p=562"},"modified":"2015-07-31T18:25:52","modified_gmt":"2015-07-31T23:25:52","slug":"sanders-cumaranatunge-minimalist-actionscript-3-factory-design-pattern","status":"publish","type":"post","link":"https:\/\/www.lonhosford.com\/lonblog\/2007\/08\/15\/sanders-cumaranatunge-minimalist-actionscript-3-factory-design-pattern\/","title":{"rendered":"Factory Design Pattern Minimalist Example For Actionscript 3  &#8211; Sanders &#038; Cumaranatunge"},"content":{"rendered":"<p><strong>By Lon (Alonzo) Hosford<\/strong><\/p>\n<p>This is the minimalist 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_Minimalist.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_Minimalist_Flash<\/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_Minimalist_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_Minimalist<\/strong><br \/>\nThis is the client class. The class simply instantiates CreatorA and CreatorB classes and calls the <code>doStuff()<\/code> method they inherit from the Creator class. The basic idea is that the ProductA and ProductB classes are uncoupled from the client. Uncoupling is a good OOP design principle.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/**\r\n * Demonstrates a minimalist example of decoupling the client, this file, from the products.\r\n * The product classes may be modified within the package and the creators maintain the\r\n * direct interface to the clients.\r\n * *\/\r\npackage\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\timport com.lonhosford.util.debug.lite.DebugMessage;\r\n\timport com.lonhosford.util.debug.lite.Debugger;\r\n\timport com.lonhosford.util.debug.lite.DebuggerEvent;\r\n\r\n\timport flash.display.Sprite;\r\n\r\n\timport example.CreatorA;\r\n\timport example.CreatorB;\r\n\r\n\t\/\/ {SET STAGE SIZE AND SPEED HERE}\r\n\t&#x5B;SWF(width=500, height = 300, frameRate = 30)]\r\n\r\n\tpublic class Chapter02_Factory_Minimalist extends Sprite\r\n\t{\r\n\t\tprivate var debugConsole:DebugConsole = DebugConsole.getInstance();\r\n\t\tprivate static const backgroundColor:Number = 0xffffff;\r\n\t\tprivate static const backgroundBorderColor:Number = 0x666666;\r\n\t\tprivate static const backgroundBorderWidth:Number = 2;\r\n\r\n\t\tpublic function Chapter02_Factory_Minimalist()\r\n\t\t{\r\n\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 Factory Minimalist Example&quot;);\r\n\t\t\tdebugConsole.write(&quot;\\n&quot;);\r\n\r\n\t\t\tvar ca:CreatorA = new CreatorA();\r\n\t\t\tvar cb:CreatorB = new CreatorB();\r\n\r\n\t\t\tca.doStuff();\r\n\t\t\tcb.doStuff();\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><strong>Creator Class<\/strong><br \/>\nThis class provides a &#8220;factory method&#8221; interface to each of the subclasses. The line 16 shows the <code>factoryMethod()<\/code> method. The method name for the example emphasizes its purpose. 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>factoryMethod()<\/code> method returns a <code>IProduct<\/code> class. Each <code>Creator<\/code> subclass will return its own product all having a manipulate()  method defined by the <code>IProduct<\/code> interface discussed later in this post. This <code>Creator<\/code> class then uses the product class <code>manipulate()<\/code> method on line 13.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage example\r\n{\r\n\timport flash.errors.IllegalOperationError;\r\n\r\n\tpublic class Creator\r\n\t{\r\n\t\tpublic function Creator()\r\n\t\t{\r\n\t\t}\r\n\t\tpublic function doStuff():void\r\n\t\t{\r\n\t\t\tvar product:IProduct = this.factoryMethod(); \/\/ this is SubClass CreatorA, CreatorB etc\r\n\t\t\tproduct.manipulate();\r\n\r\n\t\t}\r\n\t\tprotected function factoryMethod():IProduct\r\n\t\t{\r\n\t\t\tthrow new IllegalOperationError(&quot;Creator.factoryMethod() - Abstract method. 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>CreatorA Class<\/strong><br \/>\nThis is a class the client classes use. The Creater super class contains the <code>doStuff()<\/code> method clients use to interact with the product classes.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage example\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\r\n\tpublic class CreatorA extends Creator\r\n\t{\r\n\t\tprivate var debugConsole:DebugConsole = DebugConsole.getInstance();\r\n\t\tpublic function CreatorA()\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;CreatorA()&quot;);\r\n\t\t}\r\n\t\toverride protected function factoryMethod():IProduct\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;CreatorA.factoryMethod() - creating Product1&quot;);\r\n\t\t\treturn new Product1();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>CreatorB Class<\/strong><\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage example\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\r\n\tpublic class CreatorB extends Creator\r\n\t{\r\n\t\tprivate var debugConsole:DebugConsole = DebugConsole.getInstance();\r\n\t\tpublic function CreatorB()\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;CreatorB()&quot;);\r\n\t\t}\r\n\t\toverride protected function factoryMethod():IProduct\r\n\t\t{\r\n\t\t\tdebugConsole.write (&quot;CreatorB.factoryMethod() - creating Product2&quot;);\r\n\t\t\treturn new Product2();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\n<strong>IProduct Interface<\/strong><br \/>\nThis interface provides a common <code>manipulate()<\/code> method for the Creator class, and all its subclasses, in the package. Each product class implements this interface.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage example\r\n{\r\n\tpublic interface IProduct\r\n\t{\r\n\t\tfunction manipulate():void;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Product1 Class<\/strong><br \/>\nThis is the worker bee class. It must implement the <code>manipulate()<\/code> method of the IProduct interface. The Creator class uses this method in its <code>doStuff()<\/code> method. Remember the <code>doStuff()<\/code> method is how client classes interact with the products.<\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage example\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\r\n\tinternal class Product1 implements IProduct\r\n\t{\r\n\t\tprivate var debugConsole:DebugConsole = DebugConsole.getInstance();\r\n\t\tpublic function Product1()\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;Product1()&quot;);\r\n\t\t}\r\n\t\tpublic function manipulate():void\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;Product1.manipulate()&quot;);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Product2 Class<\/strong><\/p>\n<pre class=\"brush: as3; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npackage example\r\n{\r\n\timport com.lonhosford.util.debug.lite.DebugConsole;\r\n\r\n\tinternal class Product2 implements IProduct\r\n\t{\r\n\t\tprivate var debugConsole:DebugConsole = DebugConsole.getInstance();\r\n\t\tpublic function Product2()\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;Product2()&quot;);\r\n\t\t}\r\n\t\tpublic function manipulate():void\r\n\t\t{\r\n\t\t\tdebugConsole.write(&quot;Product2.manipulate()&quot;);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>By Lon (Alonzo) Hosford This is the minimalist 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 [&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,9,31,38],"class_list":["post-562","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-flex","tag-gaming","tag-sanders"],"_links":{"self":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/562","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=562"}],"version-history":[{"count":38,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":3712,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/562\/revisions\/3712"}],"wp:attachment":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/media?parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/categories?post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/tags?post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}