{"id":322,"date":"2009-06-21T19:26:23","date_gmt":"2009-06-22T02:26:23","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1606"},"modified":"2009-06-21T19:26:23","modified_gmt":"2009-06-22T02:26:23","slug":"funny-parameterized-factory","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=322","title":{"rendered":"Funny Parameterized Factory"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage{\r\n\r\n\timport flash.utils.getDefinitionByName;\r\n\t\r\n\tpublic class Creator{\r\n\t\t\r\n\t\tpublic function Creator():void{\r\n\t\t\t\/\/ classes we can create\r\n\t\t\t ClassA;\r\n\t\t\t ClassB;\r\n\t\t}\r\n\r\n\t\tpublic function create(type:String):ISomeInterface{\r\n\t\t\t var ClassRef:Class = getDefinitionByName(type) as Class;\r\n\t\t\t return ISomeInterface(new ClassRef());\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage{\r\n\tpublic interface ISomeInterface{\r\n\t\tfunction sayHi():void;\r\n\t}\r\n}\r\n<\/pre>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n package{\r\n\t\r\n\tpublic class ClassA implements ISomeInterface{\r\n\t\t\r\n\t\tpublic function ClassA(){\r\n\t\t\ttrace(&quot;new ClassA()&quot;);\r\n\t\t}\r\n\t\t\r\n\t\tpublic function sayHi():void{\r\n\t\t\ttrace(&quot;hello I am a ClassA instance&quot;);\r\n\t\t}\r\n\t}\r\n\t \r\n}\r\n<\/pre>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n package{\r\n\t\r\n\tpublic class ClassB implements ISomeInterface{\r\n\t\t\r\n\t\tpublic function ClassB(){\r\n\t\t\ttrace(&quot;new ClassB()&quot;);\r\n\t\t}\r\n\t\t\r\n\t\tpublic function sayHi():void{\r\n\t\t\ttrace(&quot;hello I am a ClassB instance&quot;);\r\n\t\t}\r\n\t}\r\n\t \r\n}\r\n<\/pre>\n<p>&#8230; and lastly on the timeline:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar creator:Creator = new Creator();\r\n\r\nvar a:ISomeInterface = creator.create(&quot;ClassA&quot;);\r\na.sayHi();\r\n\r\nvar b:ISomeInterface = creator.create(&quot;ClassB&quot;);\r\nb.sayHi();\r\n\r\n\/*outputs\r\n\r\nnew ClassA()\r\nhello I am a ClassA instance\r\nnew ClassB()\r\nhello I am a ClassB instance\r\n\r\n*\/\r\n<\/pre>\n<p>Unfortunately this has some downsides compared to a switch or if statement parameterized factory. Still fun though&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package{ import flash.utils.getDefinitionByName; public class Creator{ public function Creator():void{ \/\/ classes we can create ClassA; ClassB; } public function create(type:String):ISomeInterface{ var ClassRef:Class = getDefinitionByName(type) as Class; return ISomeInterface(new ClassRef()); } } } package{ public interface ISomeInterface{ function sayHi():void; } } package{ public class ClassA implements ISomeInterface{ public function ClassA(){ trace(&quot;new ClassA()&quot;); } public function sayHi():void{ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3,14,4],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/322\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}