{"id":470,"date":"2012-08-29T15:35:09","date_gmt":"2012-08-29T14:35:09","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=470"},"modified":"2012-09-04T15:26:35","modified_gmt":"2012-09-04T14:26:35","slug":"constructor-as-helper-in-clone-method-implementation","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2012\/08\/29\/constructor-as-helper-in-clone-method-implementation\/","title":{"rendered":"constructor as helper in clone method implementation"},"content":{"rendered":"<p>Hi,<\/p>\n<p>if you wanted to implement the clone method to create duplicates of your class you certainly face the problem in extending class where you had to duplicate the code just because the new instance should be type of the child and not parent (base) type.<br \/>\n<!--more--><\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage examples\r\n{\r\n\tpublic class A\r\n\t{\r\n\t\tprotected var m_sPropA:String = &quot;Prop A&quot;;\r\n\t\tpublic function A()\r\n\t\t{}\r\n\t\t\r\n\t\tpublic function get propA():String\r\n\t\t{\r\n\t\t\treturn m_sPropA;\r\n\t\t}\r\n\t\tpublic function set propA(value:String):void\r\n\t\t{\r\n\t\t\tm_sPropA = value;\r\n\t\t}\r\n\r\n\t\tpublic function clone():A\r\n\t\t{\r\n\t\t\tvar copy:A = new A();\r\n\t\t\t\r\n\t\t\tcopy.propA = propA;\r\n\t\t\t\r\n\t\t\treturn copy;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and another class inheriting from it<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage examples\r\n{\r\n\tpublic class B extends A\r\n\t{\r\n\t\tprotected var m_sPropB:String = &quot;Prop B&quot;;\r\n\t\tpublic function B()\r\n\t\t{}\r\n\t\t\r\n\t\tpublic function get propB():String\r\n\t\t{\r\n\t\t\treturn m_sPropB;\r\n\t\t}\r\n\t\tpublic function set propB(value:String):void\r\n\t\t{\r\n\t\t\tm_sPropB = value;\r\n\t\t}\r\n\t\t\r\n\t\toverride public function clone():A\r\n\t\t{\r\n\t\t\t\/\/you can't initiate A as it will not have propB - so you have to re write entire method duplicating whatever original method was setting (e.g. propA)\r\n\t\t\tvar copy:B = new B();\r\n\t\t\t\r\n\t\t\tcopy.propA = propA;\r\n\t\t\tcopy.propB = propB;\r\n\t\t\t\r\n\t\t\treturn copy;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>From this example, I hope you can see that when there are more classes in the chain it may be more tricky to update and prone to error, but with the constructor property you can simplify it greatly, see revised code:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage examples\r\n{\r\n\tpublic class A\r\n\t{\r\n\t\tprotected var m_sPropA:String = &quot;Prop A&quot;;\r\n\t\tpublic function A()\r\n\t\t{}\r\n\t\t\r\n\t\tpublic function get propA():String\r\n\t\t{\r\n\t\t\treturn m_sPropA;\r\n\t\t}\r\n\t\tpublic function set propA(value:String):void\r\n\t\t{\r\n\t\t\tm_sPropA = value;\r\n\t\t}\r\n\t\t\r\n\t\tpublic function clone():A\r\n\t\t{\r\n\t\t\tvar copy:A = new (this as Object).constructor();\/\/the change\r\n\t\t\t\r\n\t\t\tcopy.propA = propA;\r\n\t\t\t\r\n\t\t\treturn copy;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage examples\r\n{\r\n\tpublic class B extends A\r\n\t{\r\n\t\tprotected var m_sPropB:String = &quot;Prop B&quot;;\r\n\t\tpublic function B()\r\n\t\t{}\r\n\t\t\r\n\t\tpublic function get propB():String\r\n\t\t{\r\n\t\t\treturn m_sPropB;\r\n\t\t}\r\n\t\tpublic function set propB(value:String):void\r\n\t\t{\r\n\t\t\tm_sPropB = value;\r\n\t\t}\r\n\t\t\r\n\t\toverride public function clone():A\r\n\t\t{\r\n\t\t\tvar copy:B = super.clone() as B;\/\/the change\r\n\t\t\t\r\n\t\t\tcopy.propB = propB;\r\n\t\t\t\r\n\t\t\treturn copy;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>so all we need to implement in the clone is just those parts that are relevant to the implementing class:-)<br \/>\nbest regards<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, if you wanted to implement the clone method to create duplicates of your class you certainly face the problem in extending class where you had to duplicate the code just because the new instance should be type of the &hellip; <a href=\"https:\/\/greladesign.co\/blog\/2012\/08\/29\/constructor-as-helper-in-clone-method-implementation\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[17,9],"tags":[142,143],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/470"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":8,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":504,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions\/504"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}