{"id":9133,"date":"2020-10-12T11:00:33","date_gmt":"2020-10-12T02:00:33","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9133"},"modified":"2020-10-09T19:59:40","modified_gmt":"2020-10-09T10:59:40","slug":"builder-design-pattern-2","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9133","title":{"rendered":"Builder Design Pattern"},"content":{"rendered":"\n<p>Builder is a creative design pattern that allows you to build complex objects step by step. The pattern allows you to produce different types and representations of an object using the same building code.<\/p>\n\n\n\n<p>Imagine a complex object that requires laborious step-by-step initialization of many nested objects and fields. This initialization code is usually buried inside a monster constructor with many parameters. Or even worse: scattered throughout the client code.<\/p>\n\n\n\n<p>The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called&nbsp;<em>builders<\/em>.<\/p>\n\n\n\n<p>Lets look up the code examples I wrote as below.<\/p>\n\n\n\n<h3>PHP Code Sample<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/**\n * This is the original application interface \n * where we place the methods need to develop from its concrete sub classes\n *\/\ninterface ApplicationInterface\n{\n    public function setDbDriver(DbDriver $dbDriver);\n \n    public function query($query);\n}\n\n\/**\n * This abstract class will implements the interface as we have to reference the next hierarchy object here.\n * After that we can access the methods of their sub concrete classes in our sub classes. see below.\n *\/\nabstract class Application implements ApplicationInterface\n{\n   protected $dbDriver;\n \n   public function setDbDriver(DbDriver $dbDriver)\n   {\n       $this->dbDriver = $dbDriver;\n   }\n}\n\n\/**\n* Concrete sub classes of the original class working with the reference object's method.\n*\/\nclass android extends Application\n{\n    public function query($query)\n    {\n        $query .= \"\\n\\n running android app query\\n\";\n \n        return $this->dbDriver->handleQuery($query);\n    }\n}\n \nclass ios extends Application\n{\n    public function query($query)\n    {\n        $query .= \"\\n\\n running ios app query\\n\";\n \n        return $this->dbDriver->handleQuery($query);\n    }\n}\n\n\n\/**\n* This is the interface that need to be referenced by original class instead of inheritance.\n*\/\ninterface DbDriver\n{\n    public function handleQuery($query);\n}\n\n\/**\n* Concrete classes that will have the detail implementations of the interface.\n*\/\nclass MysqlDriver implements DbDriver\n{\n    public function handleQuery($query)\n    {\n        echo \"\\nUsing the mysql driver: \".$query;\n    }\n}\n \n \nclass OracleDriver implements DbDriver\n{\n    public function handleQuery($query)\n    {\n        echo \"\\nUsing the oracle driver: \".$query;\n    }\n}\n\n\n\/\/client code\n\/\/ client doesn't need to know any implementation details. \n\/\/ Just build the original concrete class and inject the concrete object that will be referenced.\n \n$android = new android();\n$android->setDbDriver(new MysqlDriver());\necho $android->query(\"select * from table\");\n \n$android->setDbDriver(new OracleDriver());\necho $android->query(\"select * from table\");\n \n\n$ios = new ios();\n$ios->setDbDriver(new MysqlDriver());\necho $ios->query(\"select * from table\");\n\n$ios->setDbDriver(new OracleDriver());\necho $ios->query(\"select * from table\");\n <\/code><\/pre>\n\n\n\n<h3>C# Code Sample<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace HelloWorld\n{\n    \/**\n     * Two Original classes referencing the same DbDriver which is the next hierarchy object\n     * After that we can access the methods of their sub concrete classes in our sub classes. query method in this case\n     *\/\n    class ApplicationInterface\n    {\n        protected DbDriver  _dbdriver;\n\n        public ApplicationInterface(DbDriver  dbdriver)\n        {\n            this._dbdriver = dbdriver;\n        }\n\n        public virtual string setDbDriver()\n        {\n            return \"Base DB Driver:\" +\n                _dbdriver.query();\n        }\n    }\n\n    class RefinedAbstraction : ApplicationInterface\n    {\n        public RefinedAbstraction(DbDriver  dbdriver) : base(dbdriver)\n        {\n        }\n\n        public override string setDbDriver()\n        {\n            return \"Refined DB Driver:\" +\n                base._dbdriver.query();\n        }\n    }\n\n\n    \/**\n    * This is the interface that need to be referenced by original class instead of inheritance.\n    *\/\n    public interface DbDriver \n    {\n        string query();\n    }\n\n    \/**\n    * Concrete classes that will have the detail implementations of the interface.\n    *\/\n    class MysqlDriver : DbDriver \n    {\n        public string query()\n        {\n            return \"Using the mysql driver:\\n\";\n        }\n    }\n\n    class OracleDriver : DbDriver \n    {\n        public string query()\n        {\n            return \"Using the oracle driver:.\\n\";\n        }\n    }\n\n    \/\/ client doesn't need to know any implementation details. \n    \/\/ Just build the original class and inject the concrete object that will be referenced.\n    class Client\n    {\n        public void ClientCode(ApplicationInterface applicationInterface)\n        {\n            Console.Write(applicationInterface.setDbDriver());\n        }\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Client client = new Client();\n\n            ApplicationInterface applicationInterface;\n\n            applicationInterface = new ApplicationInterface(new MysqlDriver());\n            client.ClientCode(applicationInterface);\n\n            Console.WriteLine();\n\n            applicationInterface = new RefinedAbstraction(new OracleDriver());\n            client.ClientCode(applicationInterface);\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\/9133\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9133\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Builder 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\/9133\" data-text=\"Builder 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\/9133\" 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\/9133\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Builder is a creative design pattern that allows you to build complex objects step by step. The pattern allows [&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\/9133"}],"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=9133"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9133\/revisions"}],"predecessor-version":[{"id":9136,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9133\/revisions\/9136"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}