Builder Design Pattern

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.

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.

To create an object, run a series of these steps on a constructor object. The important part is that you don’t need to call all the steps. You can call only the steps necessary to produce a particular configuration of an object.

Check out the sample codes below.

PHP sample code

<?php

/**
 * Builder interface with different methods.
 */
interface Builder
{
    public function addVege();

    public function addNoodle();

    public function addMeat();
}

/**
 * Implementing the builder and interface with different implementations
 */
class MalaBuilder implements Builder
{
    private $product;

    /**
     * To rest the Mala Object as a new one.
     */
    public function __construct()
    {
        $this->product = new Mala;
    }

    /**
     * All production steps work with the same product instance.
     */
    public function addVege()
    {
        $this->product->ingredients[] = "Vegetable";
    }

    public function addNoodle()
    {
        $this->product->ingredients[] = "Noodle";
    }

    public function addMeat()
    {
        $this->product->ingredients[] = "Meat";
    }

    /**
     * returning results for each different implementatins.
     */
    public function add()
    {
        $result = $this->product;

         /**
         * To rest the Mala Object as a new one.
         */
        $this->product = new Mala;

        return $result;
    }
}

/**
 * display the final results of Malabuilder
 */
class Mala
{
    public $ingredients = [];

    public function returnMala()
    {
        echo implode(', ', $this->ingredients) . "<br>";
    }
}

/**
 * I added a sample MalaDirector to create a few ready made implementations.
 */
class MalaDirector
{
    private $builder;

    public function setBuilder(Builder $builder)
    {
        $this->builder = $builder;
    }

    public function addVegeNoodle()
    {
        $this->builder->addVege();
        $this->builder->addNoodle();
    }

    public function addVegeMeatNoodle()
    {
        $this->builder->addVege();
        $this->builder->addNoodle();
        $this->builder->addMeat();
    }
}


function clientCode(MalaDirector $malaDirector)
{
    $builder = new MalaBuilder;
    $malaDirector->setBuilder($builder);

    echo "This is vege Mala:\n";
    $malaDirector->addVegeNoodle();
    $builder->add()->returnMala();

    echo "Full Ingredients Mala:\n";
    $malaDirector->addVegeMeatNoodle();
    $builder->add()->returnMala();

    // This time with no MalaDirector usage.
    echo "Custom Mala:\n";
    $builder->addNoodle();
    $builder->addMeat();
    $builder->add()->returnMala();
}

$malaDirector = new MalaDirector;
clientCode($malaDirector);

C# sample code

using System;
using System.Collections.Generic;

namespace HelloWorld
{
    /**
     * Builder interface with different methods.
     */
    public interface Builder
    {
        void addVege();

        void addNoodle();

        void addMeat();
    }

    /**
     * Implementing the builder and interface with different implementations
     */
    public class MalaBuilder : Builder
    {
        private Mala _mala = new Mala();

        /**
         * To rest the Mala Object as a new one.
         */
        public MalaBuilder()
        {
            this._mala = new Mala();
        }

        public void addVege()
        {
            this._mala.Add("vegetable");
        }

        public void addNoodle()
        {
            this._mala.Add("noodle");
        }

        public void addMeat()
        {
            this._mala.Add("meat");
        }

        /**
         * returning results for each different implementatins.
         */
        public Mala FinalMala()
        {
            Mala result = this._mala;

            this._mala = new Mala();

            return result;
        }
    }

    /**
     * add function to the list object of ingredients.
     * return the final results of Malabuilder.
     */
    public class Mala
    {
        private List<object> incredigents = new List<object>();

        public void Add(string part)
        {
            this.incredigents.Add(part);
        }

        public string ReturnMala()
        {
            string str = string.Empty;

            for (int i = 0; i < this.incredigents.Count; i++)
            {
                str += this.incredigents[i] + ", ";
            }

            str = str.Remove(str.Length - 2); //removing last comma and space.

            return + str + "\n";
        }
    }

    /**
     * A sample MalaDirector to create a few ready made implementations.
     */
    public class Director
    {
        private Builder _builder;

        public Builder Builder
        {
            set { _builder = value; }
        }

        public void addVegeNoodle()
        {
            this._builder.addVege();
        }

        public void addVegeMeatNoodle()
        {
            this._builder.addVege();
            this._builder.addNoodle();
            this._builder.addMeat();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var director = new Director();
            var builder = new MalaBuilder();
            director.Builder = builder;

            Console.WriteLine("Vegetable Mala:");
            director.addVegeNoodle();
            Console.WriteLine(builder.FinalMala().ReturnMala());

            Console.WriteLine("Full Ingredients Mala:");
            director.addVegeMeatNoodle();
            Console.WriteLine(builder.FinalMala().ReturnMala());

            // This time with no MalaDirector usage.
            Console.WriteLine("Custom Mala:");
            builder.addNoodle();
            builder.addMeat();
            Console.Write(builder.FinalMala().ReturnMala());
        }
    }

}

By Yuuma



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム