{"id":8936,"date":"2020-08-03T11:07:22","date_gmt":"2020-08-03T02:07:22","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=8936"},"modified":"2020-08-03T12:35:28","modified_gmt":"2020-08-03T03:35:28","slug":"factory-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/8936","title":{"rendered":"Factory Design Pattern"},"content":{"rendered":"\n<p>Factory Method is a creation design pattern that provides an interface for creating objects and allows subclasses to alter the type of objects that will be created.<\/p>\n\n\n\n<p>Imagine that you\u2019re implementing a mailing feature . At first you are sending email with mailgun and all of your business source code are all in one place.<\/p>\n\n\n\n<p>Later you have to implement using another service called mailchimp. So you have to overwrite you source code again. If you are going to use both of the services, your code will be messy to be able to work for both services.<\/p>\n\n\n\n<p>At this point , we can use factory design pattern, by creating objects for both service classes. So, we gonna build a mailing interface with an abstract function `sendmail` first. Then we will have two sub classes to implement that interface (mailgun and mailchimp classes for now). Inside these classes, there will be a function to override the abstract function from interface called `sendmail` which will be different in implementation according to classes.<\/p>\n\n\n\n<p>At the final step, we will create factory classes and return mail implementation concrete classes. The client just has to choose which factory to be used, doesn&#8217;t has to know the detail implementation of the concrete classes which is the main theme of this factory pattern.<\/p>\n\n\n\n<p>So I will create code samples for the above scenarios. I will write in PHP &amp; C#.<\/p>\n\n\n\n<p>PHP Sample Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nabstract class MailerFactory\n{\n    abstract function mailDriver();\n}\nclass MailchimpFactory extends MailerFactory\n{\n    public function mailDriver()\n    {\n        return new MailchimpMailer();\n    }\n}\nclass MailgunFactory extends MailerFactory\n{\n    public function mailDriver()\n    {\n        return new MailgunMailer();\n    }\n}\n\ninterface Mailer\n{\n    function sendmail($message);\n}\nclass MailchimpMailer implements Mailer\n{\n    public function sendmail($message)\n    {\n        echo(\"Mailchimp email &gt; \" . $message . \"&lt;br\/&gt;\");\n    }\n}\nclass MailgunMailer implements Mailer\n{\n    public function sendmail($message)\n    {\n        echo(\"mailgun email &gt; \" . $message . \"&lt;br\/&gt;\");\n    }\n}\n\n\/\/Client code\n$client = new MailchimpFactory();\n$client-&gt;mailDriver()-&gt;sendmail(\"This is from Mailchimp\");\n$client = new MailgunFactory();\n$client-&gt;mailDriver()-&gt;sendmail(\"this is from mailgun\");\n?&gt;<\/code><\/pre>\n\n\n\n<p>C# sample code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace HelloWorld\n{\n    abstract class MailerFactory\n    {\n        public abstract Mailer mailDriver();\n    }\n\n    class MailchimpFactory : MailerFactory\n    {\n        public override Mailer mailDriver()\n        {\n            return new MailchimpMailer();\n        }\n    }\n\n    class MailgunFactory : MailerFactory\n    {\n        public override Mailer mailDriver()\n        {\n            return new MailgunMailer();\n        }\n    }\n\n    public interface Mailer\n    {\n        string sendmail(string message);\n    }\n\n    class MailchimpMailer : Mailer\n    {\n        public string sendmail(string message)\n        {\n            return \"sending from mailchimp\";\n        }\n    }\n\n    class MailgunMailer : Mailer\n    {\n        public string sendmail(string message)\n        {\n            return \"sending from mailgun\";\n        }\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var mailgun = new MailgunFactory();\n            Console.WriteLine(mailgun.mailDriver().sendmail(\"sending from mailgun\"));\n\n\n            var mailchimp = new MailchimpFactory();\n            Console.WriteLine(mailchimp.mailDriver().sendmail(\"sending from mailchimp\"));\n        }\n    }\n\n}<\/code><\/pre>\n\n\n\n<h4>Pros<\/h4>\n\n\n\n<ul><li>loosely coupling as classes are not depending on each other.<\/li><li>easy to maintain (extend or remove) concrete classes.<\/li><\/ul>\n\n\n\n<h4>Cons<\/h4>\n\n\n\n<ul><li>Code might be nasty when many concrete classes are implementing to one interface.<\/li><\/ul>\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\/8936\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/8936\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Factory 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\/8936\" data-text=\"Factory 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\/8936\" 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\/8936\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Factory Method is a creation design pattern that provides an interface for creating objects and allows subclas [&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\/8936"}],"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=8936"}],"version-history":[{"count":5,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8936\/revisions"}],"predecessor-version":[{"id":8945,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8936\/revisions\/8945"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=8936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=8936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=8936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}