{"id":9107,"date":"2020-10-05T11:00:21","date_gmt":"2020-10-05T02:00:21","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9107"},"modified":"2020-10-02T19:05:55","modified_gmt":"2020-10-02T10:05:55","slug":"prototype-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9107","title":{"rendered":"Prototype Design Pattern"},"content":{"rendered":"\n<p>Prototype is a build design pattern that allows you to copy existing objects without making your code dependent on their classes.<\/p>\n\n\n\n<p>Declare an abstract base class that specifies a pure virtual &#8220;clone&#8221; method, and maintain a dictionary of all concrete &#8220;cloneable&#8221; derived classes. Any class that needs a &#8220;polymorphic constructor&#8221; capability: It derives from the abstract base class, registers its prototypical instance, and implements the clone () operation.<\/p>\n\n\n\n<p>So the client, instead of writing code that invokes the &#8220;new&#8221; operator on a wired class name, calls a &#8220;clone&#8221; operation on the abstract base class, supplying an enumerated string or data type that designates the class desired derivative. <\/p>\n\n\n\n<p>Lets see PHP &amp; C# code samples as below.<\/p>\n\n\n\n<h3>PHP<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/ I made a common prototype interface for cloning purpose.\n\/\/ has a single function called _clone\nabstract class Person {\n\n    protected $name;\n\n    abstract function __clone();\n\n    function getName() {\n        return $this->name;\n    }\n    function setName($name) {\n        $this->name = $name;\n    }\n}\n\n\/\/ Here is a concrete class extending the person prototype.\n\/\/ we will implement the _clone method to support object cloning.\n\/\/This may contain a lot of fields and implementations but I will keep this simple for now.\nclass Boy extends Person {\n    function __clone() {\n\n    }\n}\n\n\n\/\/here is our first time instantiation of boy concrete class\n$person = new Boy();\n\n\/\/next time we will clone that object by using clone.\n\/\/ we will also pass the name param dynamically.\n\/\/ clone1\n$person1 = clone $person;\n$person1->setName('Hlaing');\n$person1->getName();\n\n\/\/ clone2\n$person2 = clone $boy;\n$person2->setName('Tin');\n$person2->getName();\n\nprint_r($person1);\nprint_r($person2);\n \n?>\n<\/code><\/pre>\n\n\n\n<h3><a href=\"https:\/\/github.com\/HlaingTinHtun\/GOF-Design-Patterns\/blob\/master\/articles\/GOF_Patterns\/creational_patterns\/prototype.md#c\"><\/a>C#<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace HelloWorld\n{\n    \/\/ Here is my simple object that support object cloning.\n    \/\/ I already build a function called copyclone to clone this object.\n    public class Person\n    {\n        public string Name;\n\n        public Person CopyClone()\n        {\n            return (Person)this.MemberwiseClone();\n        }\n    }\n\n    \n    class Program\n    {\n        static void Main(string[] args)\n        {\n            \/\/ firstly we build a object using new instantiation\n            Person person = new Person();\n\n            \/\/next time we will clone that object by theh function copyclone.\n            \/\/ we will also pass the name param dynamically.\n            \/\/ clone1\n            Person person1 = person.CopyClone();\n            person1.Name = \"Hlaing\";\n\n            \/\/ clone2\n            Person person2 = person.CopyClone();\n            person2.Name = \"Tin\";\n\n\n            Console.WriteLine(person1.Name);\n            Console.WriteLine(person2.Name);\n        }   \n    }\n}\n<\/code><\/pre>\n\n\n\n<p>By Yuuma.<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9107\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9107\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Prototype Design Pattern\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9107\" data-text=\"Prototype Design Pattern\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9107\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9107\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Prototype is a build design pattern that allows you to copy existing objects without making your code dependen [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9107"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9107"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9107\/revisions"}],"predecessor-version":[{"id":9111,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9107\/revisions\/9111"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}