{"id":8969,"date":"2020-08-24T11:05:10","date_gmt":"2020-08-24T02:05:10","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=8969"},"modified":"2020-08-21T19:24:12","modified_gmt":"2020-08-21T10:24:12","slug":"abstract-factory-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/8969","title":{"rendered":"Abstract Factory Design Pattern"},"content":{"rendered":"\n<p>Abstract Factory defines an interface to create all the different products, but leaves the actual creation of the product to specific factory classes. Each type of factory corresponds to a certain variety of product.<\/p>\n\n\n\n<p>The client code calls the methods of creating a factory object instead of creating products directly with a call to the constructor (new operator). Since a factory corresponds to a single product variant, all of its products will be compatible.<\/p>\n\n\n\n<p>The client code works with factories and products only through their abstract interfaces. This allows the client code to work with any product variant, created by the factory object. <\/p>\n\n\n\n<p><font style=\"vertical-align: inherit\"><font style=\"vertical-align: inherit\">C# code sample<\/font><\/font><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace HelloWorld\n{\n    \/**\n * Define the Factory interface first.\n *\/\n    abstract class TvSetFactory\n    {\n        public abstract Tv createTv();\n        public abstract CdPlayer createCdPlayer();\n    }\n\n    \/**\n     * Then I will create related factory classes and implements the interface\n     * Panasonic  Factory &amp; SamsaungFactory in this scenario.\n     *\/\n    class PanasonicFactory : TvSetFactory\n    {\n        public override Tv createTv()\n        {\n            return new PanasonicTv();\n        }\n\n        public override CdPlayer createCdPlayer()\n        {\n            return new PanasonicCdPlayer();\n        }\n    }\n\n    class SamsaungFactory : TvSetFactory\n    {\n        public override Tv createTv()\n        {\n            return new SamsaungTv();\n        }\n\n        public override CdPlayer createCdPlayer()\n        {\n            return new SamsaungCdPlayer();\n        }\n\n    }\n\n    \/**\n     * Now lets build family of object classes.\n     * Tv &amp; CdPlayer in this scenario.\n     *\/\n    abstract class Tv\n    {\n        public abstract string getTv();\n    }\n\n    \/**\n     * These classes have to be implemented by their relative factory.\n     *\/\n    class PanasonicTv : Tv\n    {\n        public override string getTv()\n        {\n            return \"PanasonicTv\";\n        }\n    }\n\n    class SamsaungTv : Tv\n    {\n        public override string getTv()\n        {\n            return \"SamsaungTv\";\n        }\n    }\n\n    abstract class CdPlayer\n    {\n        public abstract string getCdPlayer();\n    }\n\n    class PanasonicCdPlayer : CdPlayer\n    {\n        public override string getCdPlayer()\n        {\n            return \"PanasonicCdPlayer\";\n        }\n    }\n\n    class SamsaungCdPlayer : CdPlayer\n    {\n        public override string getCdPlayer()\n        {\n            return \"SamsaungCdPlayer\";\n        }\n    }\n\n\n    \/\/ client code\n    \/**\n     * Now you can call the specific factory to return family of objects without knowing their own detail implementations.\n     * if you want to get samsaung products, you just need to change the Factory class to samsaung factory.\n     *\/\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var tv = new PanasonicFactory();\n            Console.WriteLine(tv.createTv().getTv());\n\n            var cdPlayer = new PanasonicFactory();\n            Console.WriteLine(cdPlayer.createCdPlayer().getCdPlayer());\n\n        }\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>PHP code sample<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/**\n * Define the Factory interface first.\n *\/\nabstract class TvSetFactory\n{\n    abstract function createTv();\n    abstract function createCdPlayer();\n}\n\n\/**\n * Then I will create related factory classes and extends the interface\n * Panasonic  Factory &amp; SamsaungFactory in this scenario.\n *\/\nclass PanasonicFactory extends TvSetFactory\n{\n    public function createTv()\n    {\n        return new PanasonicTv;\n    }\n\n    public function createCdPlayer()\n    {\n        return new PanasonicCdPlayer();\n    }\n}\n\nclass SamsaungFactory extends TvSetFactory\n{\n    public function createTv()\n    {\n        return new SamsaungTv;\n    }\n\n    public function createCdPlayer()\n    {\n        return new SamsaungCdPlayer();\n    }\n\n}\n\n\/**\n * Now lets build family of object classes.\n * Tv &amp; CdPlayer in this scenario.\n *\/\nabstract class Tv\n{\n    abstract function getTv();\n}\n\n\/**\n * These classes have to be implemented by their relative factory.\n *\/\nclass PanasonicTv extends Tv\n{\n    public function getTv()\n    {\n        return \"PanasonicTv\";\n    }\n}\n\nclass SamsaungTv extends Tv\n{\n    public function getTv(): string\n    {\n        return \"SamsaungTv\";\n    }\n}\n\nabstract class CdPlayer\n{\n    abstract function getCdPlayer();\n}\n\nclass PanasonicCdPlayer extends CdPlayer\n{\n    public function getCdPlayer()\n    {\n        return \"PanasonicCdPlayer\";\n    }\n}\n\nclass SamsaungCdPlayer extends CdPlayer\n{\n  public function getCdPlayer()\n  {\n      return \"SamsaungCdPlayer\";\n  }\n}\n\n\n\/\/ client code\nfunction test($tvSetFactory)\n{\n    $tv = $tvSetFactory->createTv();\n    echo('TV model is'.$tv->getTv());\n\n    $cdPlayer = $tvSetFactory->createCdPlayer();\n    echo('CD Player model is'.$cdPlayer->getCdPlayer());\n}\n\n\/**\n * Now you can call the specific factory to return family of objects without knowing their own detail implementations.\n * if you want to get samsaung products, you just need to change the Factory class to samsaung factory.\n *\/\n$tvSetFactory = new PanasonicFactory;\ntest($tvSetFactory);<\/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\/8969\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/8969\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Abstract 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\/8969\" data-text=\"Abstract 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\/8969\" 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\/8969\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Abstract Factory defines an interface to create all the different products, but leaves the actual creation of  [&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\/8969"}],"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=8969"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8969\/revisions"}],"predecessor-version":[{"id":8971,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8969\/revisions\/8971"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=8969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=8969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=8969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}