{"id":9091,"date":"2020-09-28T11:00:41","date_gmt":"2020-09-28T02:00:41","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9091"},"modified":"2020-09-25T19:17:02","modified_gmt":"2020-09-25T10:17:02","slug":"builder-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9091","title":{"rendered":"Builder Design Pattern"},"content":{"rendered":"\n<p>Builder is a creational 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>The Builder pattern suggests that you extract the building code from the object of its own class and move it to separate objects called constructors.<\/p>\n\n\n\n<p>To create an object, run a series of these steps on a constructor object. The important part is that you don&#8217;t need to call all the steps. You can call only the steps necessary to produce a particular configuration of an object.<\/p>\n\n\n\n<p>Check out the sample codes below.<\/p>\n\n\n\n<h3>PHP sample code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/**\n * Builder interface with different methods.\n *\/\ninterface Builder\n{\n    public function addVege();\n\n    public function addNoodle();\n\n    public function addMeat();\n}\n\n\/**\n * Implementing the builder and interface with different implementations\n *\/\nclass MalaBuilder implements Builder\n{\n    private $product;\n\n    \/**\n     * To rest the Mala Object as a new one.\n     *\/\n    public function __construct()\n    {\n        $this->product = new Mala;\n    }\n\n    \/**\n     * All production steps work with the same product instance.\n     *\/\n    public function addVege()\n    {\n        $this->product->ingredients[] = \"Vegetable\";\n    }\n\n    public function addNoodle()\n    {\n        $this->product->ingredients[] = \"Noodle\";\n    }\n\n    public function addMeat()\n    {\n        $this->product->ingredients[] = \"Meat\";\n    }\n\n    \/**\n     * returning results for each different implementatins.\n     *\/\n    public function add()\n    {\n        $result = $this->product;\n\n         \/**\n         * To rest the Mala Object as a new one.\n         *\/\n        $this->product = new Mala;\n\n        return $result;\n    }\n}\n\n\/**\n * display the final results of Malabuilder\n *\/\nclass Mala\n{\n    public $ingredients = [];\n\n    public function returnMala()\n    {\n        echo implode(', ', $this->ingredients) . \"&lt;br>\";\n    }\n}\n\n\/**\n * I added a sample MalaDirector to create a few ready made implementations.\n *\/\nclass MalaDirector\n{\n    private $builder;\n\n    public function setBuilder(Builder $builder)\n    {\n        $this->builder = $builder;\n    }\n\n    public function addVegeNoodle()\n    {\n        $this->builder->addVege();\n        $this->builder->addNoodle();\n    }\n\n    public function addVegeMeatNoodle()\n    {\n        $this->builder->addVege();\n        $this->builder->addNoodle();\n        $this->builder->addMeat();\n    }\n}\n\n\nfunction clientCode(MalaDirector $malaDirector)\n{\n    $builder = new MalaBuilder;\n    $malaDirector->setBuilder($builder);\n\n    echo \"This is vege Mala:\\n\";\n    $malaDirector->addVegeNoodle();\n    $builder->add()->returnMala();\n\n    echo \"Full Ingredients Mala:\\n\";\n    $malaDirector->addVegeMeatNoodle();\n    $builder->add()->returnMala();\n\n    \/\/ This time with no MalaDirector usage.\n    echo \"Custom Mala:\\n\";\n    $builder->addNoodle();\n    $builder->addMeat();\n    $builder->add()->returnMala();\n}\n\n$malaDirector = new MalaDirector;\nclientCode($malaDirector);<\/code><\/pre>\n\n\n\n<h3>C# sample code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace HelloWorld\n{\n    \/**\n     * Builder interface with different methods.\n     *\/\n    public interface Builder\n    {\n        void addVege();\n\n        void addNoodle();\n\n        void addMeat();\n    }\n\n    \/**\n     * Implementing the builder and interface with different implementations\n     *\/\n    public class MalaBuilder : Builder\n    {\n        private Mala _mala = new Mala();\n\n        \/**\n         * To rest the Mala Object as a new one.\n         *\/\n        public MalaBuilder()\n        {\n            this._mala = new Mala();\n        }\n\n        public void addVege()\n        {\n            this._mala.Add(\"vegetable\");\n        }\n\n        public void addNoodle()\n        {\n            this._mala.Add(\"noodle\");\n        }\n\n        public void addMeat()\n        {\n            this._mala.Add(\"meat\");\n        }\n\n        \/**\n         * returning results for each different implementatins.\n         *\/\n        public Mala FinalMala()\n        {\n            Mala result = this._mala;\n\n            this._mala = new Mala();\n\n            return result;\n        }\n    }\n\n    \/**\n     * add function to the list object of ingredients.\n     * return the final results of Malabuilder.\n     *\/\n    public class Mala\n    {\n        private List&lt;object> incredigents = new List&lt;object>();\n\n        public void Add(string part)\n        {\n            this.incredigents.Add(part);\n        }\n\n        public string ReturnMala()\n        {\n            string str = string.Empty;\n\n            for (int i = 0; i &lt; this.incredigents.Count; i++)\n            {\n                str += this.incredigents[i] + \", \";\n            }\n\n            str = str.Remove(str.Length - 2); \/\/removing last comma and space.\n\n            return + str + \"\\n\";\n        }\n    }\n\n    \/**\n     * A sample MalaDirector to create a few ready made implementations.\n     *\/\n    public class Director\n    {\n        private Builder _builder;\n\n        public Builder Builder\n        {\n            set { _builder = value; }\n        }\n\n        public void addVegeNoodle()\n        {\n            this._builder.addVege();\n        }\n\n        public void addVegeMeatNoodle()\n        {\n            this._builder.addVege();\n            this._builder.addNoodle();\n            this._builder.addMeat();\n        }\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var director = new Director();\n            var builder = new MalaBuilder();\n            director.Builder = builder;\n\n            Console.WriteLine(\"Vegetable Mala:\");\n            director.addVegeNoodle();\n            Console.WriteLine(builder.FinalMala().ReturnMala());\n\n            Console.WriteLine(\"Full Ingredients Mala:\");\n            director.addVegeMeatNoodle();\n            Console.WriteLine(builder.FinalMala().ReturnMala());\n\n            \/\/ This time with no MalaDirector usage.\n            Console.WriteLine(\"Custom Mala:\");\n            builder.addNoodle();\n            builder.addMeat();\n            Console.Write(builder.FinalMala().ReturnMala());\n        }\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\/9091\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9091\" 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\/9091\" 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\/9091\" 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\/9091\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Builder is a creational design pattern that allows you to build complex objects step by step. The pattern allo [&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\/9091"}],"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=9091"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9091\/revisions"}],"predecessor-version":[{"id":9095,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9091\/revisions\/9095"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}