Skip to content

Instantly share code, notes, and snippets.

@firefoxrebo
Last active September 10, 2023 21:31
Show Gist options
  • Select an option

  • Save firefoxrebo/f78ab3c352b77279c94e208570eaf187 to your computer and use it in GitHub Desktop.

Select an option

Save firefoxrebo/f78ab3c352b77279c94e208570eaf187 to your computer and use it in GitHub Desktop.

Revisions

  1. firefoxrebo revised this gist Mar 29, 2017. 1 changed file with 158 additions and 84 deletions.
    242 changes: 158 additions & 84 deletions builder.php
    Original file line number Diff line number Diff line change
    @@ -1,175 +1,249 @@
    <?php

    // Explaining the Builder Design Pattern
    // The Director
    class SmartPhoneDirector
    // Builder Design Pattern


    // Product
    class SmartPhone
    {
    public function buildMobile(MobileBuilder $builder, array $options)
    public $cpu;
    public $gpu;
    public $ram;
    public $sensors;
    public $camera;
    public $case;

    }

    // Concrete Products
    class SamsungPhone extends SmartPhone
    {
    public function __toString()
    {
    $builder->addCPU($options['cpu']);
    $builder->addRam($options['ram']);
    $builder->addWrapper($options['wrapper']);
    $builder->addGPU($options['gpu']);
    $builder->addSensors($options['sensors']);
    $builder->addCamera($options['camera']);
    return '<h1>Your Samsung Phone is Ready with the following Specs.</h1>' . '<pre>' . var_export($this, true) . '</pre>';
    }
    }

    return $builder->getPhone();
    class HTCPhone extends SmartPhone
    {
    public function __toString()
    {
    return '<h1>Your HTC Phone is Ready with the following Specs.</h1>' . '<pre>' . var_export($this, true) . '</pre>';
    }
    }

    class ApplePhone extends SmartPhone
    {
    public function __toString()
    {
    return '<h1>Your Apple Phone is Ready with the following Specs.</h1>' . '<pre>' . var_export($this, true) . '</pre>';
    }
    }

    // The Builder Interface
    interface MobileBuilder
    // Builder Interface
    interface SmartPhoneBuilder
    {
    public function addCPU($cpu);
    public function addRam($ram);
    public function addWrapper($wrapper);
    public function addGPU($gpu);
    public function addSensors($sensors);
    public function addCamera($camera);

    public function getPhone();
    public function addCPU();
    public function addGPU();
    public function addRAM();
    public function addSensors();
    public function addCamera();
    public function addCase();
    public function getProduct();
    }

    // Concrete Builder
    class SamsungPhoneBuilder implements MobileBuilder
    // Concrete Builders
    class SamsungPhoneBuilder implements SmartPhoneBuilder
    {

    public $product;
    private $product;
    private $options;

    public function __construct()
    public function __construct(array $options)
    {
    $this->options = $options;
    $this->product = new SamsungPhone();
    }

    public function addCPU($cpu)
    public function addCPU()
    {
    $this->product->cpu = $cpu;
    $this->product->cpu = $this->options['cpu'];
    }

    public function addRam($ram)
    public function addGPU()
    {
    $this->product->ram = $ram;
    $this->product->gpu = $this->options['gpu'];
    }

    public function addWrapper($wrapper)
    public function addRAM()
    {
    $this->product->wrapper = $wrapper;
    $this->product->ram = $this->options['ram'];
    }

    public function addGPU($gpu)
    public function addSensors()
    {
    $this->product->gpu = $gpu;
    $this->product->sensors = $this->options['sensors'];
    }

    public function addCamera($camera)
    public function addCamera()
    {
    $this->product->camera = $camera;
    $this->product->camera = $this->options['camera'];
    }

    public function addSensors($sensors)
    public function addCase()
    {
    $this->product->sensors = $sensors;
    $this->product->case = $this->options['case'];
    }

    public function getPhone()
    public function getProduct()
    {
    return $this->product;
    }

    }

    // Concrete Builder
    class ApplePhoneBuilder implements MobileBuilder
    class HTCPhoneBuilder implements SmartPhoneBuilder
    {
    public $product;

    public function __construct()
    private $product;
    private $options;

    public function __construct(array $options)
    {
    $this->product = new iPhonePhone();
    $this->options = $options;
    $this->product = new HTCPhone();
    }

    public function addCPU($cpu)
    public function addCPU()
    {
    $this->product->cpu = $cpu;
    $this->product->cpu = $this->options['cpu'];
    }

    public function addRam($ram)
    public function addGPU()
    {
    $this->product->ram = $ram;
    $this->product->gpu = $this->options['gpu'];
    }

    public function addWrapper($wrapper)
    public function addRAM()
    {
    $this->product->wrapper = $wrapper;
    $this->product->ram = $this->options['ram'];
    }

    public function addGPU($gpu)
    public function addSensors()
    {
    $this->product->gpu = $gpu;
    $this->product->sensors = $this->options['sensors'];
    }

    public function addCamera($camera)
    public function addCamera()
    {
    $this->product->camera = $camera;
    $this->product->camera = $this->options['camera'];
    }

    public function addSensors($sensors)
    public function addCase()
    {
    $this->product->sensors = $sensors;
    $this->product->case = $this->options['case'];
    }

    public function getPhone()
    public function getProduct()
    {
    return $this->product;
    }
    }

    class SmartPhone
    class ApplePhoneBuilder implements SmartPhoneBuilder
    {
    public $cpu;
    public $ram;
    public $sensors;
    public $gpu;
    public $wrapper;
    public $camera;
    }

    class SamsungPhone extends SmartPhone
    {
    public function __toString()
    private $product;
    private $options;

    public function __construct(array $options)
    {
    $this->options = $options;
    $this->product = new ApplePhone();
    }

    public function addCPU()
    {
    $this->product->cpu = $this->options['cpu'];
    }

    public function addGPU()
    {
    $this->product->gpu = $this->options['gpu'];
    }

    public function addRAM()
    {
    $this->product->ram = $this->options['ram'];
    }

    public function addSensors()
    {
    $this->product->sensors = $this->options['sensors'];
    }

    public function addCamera()
    {
    $this->product->camera = $this->options['camera'];
    }

    public function addCase()
    {
    $this->product->case = $this->options['case'];
    }

    public function getProduct()
    {
    return '<p>Your Samsung Mobile is Ready: </p><pre>' . var_export($this, true) . '</pre>';
    return $this->product;
    }
    }

    class iPhonePhone extends SmartPhone
    // Director
    class SmartPhoneCreator
    {
    public function __toString()
    public function buildPhone(SmartPhoneBuilder $builder)
    {
    return '<p>Your iPhone Mobile is Ready: </p><pre>' . var_export($this, true) . '</pre>';
    $builder->addCPU();
    $builder->addGPU();
    $builder->addRAM();
    $builder->addSensors();
    $builder->addCamera();
    $builder->addCase();

    return $builder->getProduct();
    }
    }

    $creator = new SmartPhoneCreator();

    $samsungBuilder = new SamsungPhoneBuilder();
    $iphoneBuilder = new ApplePhoneBuilder();
    $director = new SmartPhoneDirector();
    $note3 = $director->buildMobile($samsungBuilder, [
    $samsungBuilder = new SamsungPhoneBuilder([
    'cpu' => 'Snapdragon 820',
    'gpu' => 'Adreno 530',
    'sensors' => ['UV', 'GPS', 'Barometer', 'Altimeter', 'GLONASS'],
    'ram' => 4,
    'sensors' => ['UV', 'Barometer', 'Altimeter', 'GPS', 'GLONASS'],
    'camera' => 12,
    'wrapper' => 'Silver Case'
    'case' => 'Silver Metalic Case'
    ]);
    $iPhone = $director->buildMobile($iphoneBuilder, [
    $appleBuilder = new ApplePhoneBuilder([
    'cpu' => 'A10',
    'gpu' => 'Apple GPU',
    'sensors' => ['UV', 'GPS', 'Barometer', 'Altimeter', 'GLONASS'],
    'gpu' => 'Apple',
    'ram' => 3,
    'sensors' => ['UV', 'Barometer', 'Altimeter', 'GPS', 'GLONASS'],
    'camera' => 12,
    'wrapper' => 'Black Case'
    'case' => 'Black Matt Case'
    ]);
    $htcBuilder = new HTCPhoneBuilder([
    'cpu' => 'Snapdragon 810',
    'gpu' => 'Adreno 520',
    'ram' => 4,
    'sensors' => ['UV', 'Barometer', 'Altimeter', 'GPS'],
    'camera' => 8,
    'case' => 'Blue Matt Case'
    ]);

    echo $note3;
    echo $iPhone;
    $note7 = $creator->buildPhone($samsungBuilder);
    $iphone7Plus = $creator->buildPhone($appleBuilder);
    $htc = $creator->buildPhone($htcBuilder);

    echo $note7;
    echo $iphone7Plus;
    echo $htc;
  2. firefoxrebo renamed this gist Mar 29, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. firefoxrebo created this gist Mar 29, 2017.
    175 changes: 175 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,175 @@
    <?php

    // Explaining the Builder Design Pattern
    // The Director
    class SmartPhoneDirector
    {
    public function buildMobile(MobileBuilder $builder, array $options)
    {
    $builder->addCPU($options['cpu']);
    $builder->addRam($options['ram']);
    $builder->addWrapper($options['wrapper']);
    $builder->addGPU($options['gpu']);
    $builder->addSensors($options['sensors']);
    $builder->addCamera($options['camera']);

    return $builder->getPhone();
    }
    }

    // The Builder Interface
    interface MobileBuilder
    {
    public function addCPU($cpu);
    public function addRam($ram);
    public function addWrapper($wrapper);
    public function addGPU($gpu);
    public function addSensors($sensors);
    public function addCamera($camera);

    public function getPhone();
    }

    // Concrete Builder
    class SamsungPhoneBuilder implements MobileBuilder
    {

    public $product;

    public function __construct()
    {
    $this->product = new SamsungPhone();
    }

    public function addCPU($cpu)
    {
    $this->product->cpu = $cpu;
    }

    public function addRam($ram)
    {
    $this->product->ram = $ram;
    }

    public function addWrapper($wrapper)
    {
    $this->product->wrapper = $wrapper;
    }

    public function addGPU($gpu)
    {
    $this->product->gpu = $gpu;
    }

    public function addCamera($camera)
    {
    $this->product->camera = $camera;
    }

    public function addSensors($sensors)
    {
    $this->product->sensors = $sensors;
    }

    public function getPhone()
    {
    return $this->product;
    }

    }

    // Concrete Builder
    class ApplePhoneBuilder implements MobileBuilder
    {
    public $product;

    public function __construct()
    {
    $this->product = new iPhonePhone();
    }

    public function addCPU($cpu)
    {
    $this->product->cpu = $cpu;
    }

    public function addRam($ram)
    {
    $this->product->ram = $ram;
    }

    public function addWrapper($wrapper)
    {
    $this->product->wrapper = $wrapper;
    }

    public function addGPU($gpu)
    {
    $this->product->gpu = $gpu;
    }

    public function addCamera($camera)
    {
    $this->product->camera = $camera;
    }

    public function addSensors($sensors)
    {
    $this->product->sensors = $sensors;
    }

    public function getPhone()
    {
    return $this->product;
    }
    }

    class SmartPhone
    {
    public $cpu;
    public $ram;
    public $sensors;
    public $gpu;
    public $wrapper;
    public $camera;
    }

    class SamsungPhone extends SmartPhone
    {
    public function __toString()
    {
    return '<p>Your Samsung Mobile is Ready: </p><pre>' . var_export($this, true) . '</pre>';
    }
    }

    class iPhonePhone extends SmartPhone
    {
    public function __toString()
    {
    return '<p>Your iPhone Mobile is Ready: </p><pre>' . var_export($this, true) . '</pre>';
    }
    }


    $samsungBuilder = new SamsungPhoneBuilder();
    $iphoneBuilder = new ApplePhoneBuilder();
    $director = new SmartPhoneDirector();
    $note3 = $director->buildMobile($samsungBuilder, [
    'cpu' => 'Snapdragon 820',
    'gpu' => 'Adreno 530',
    'sensors' => ['UV', 'GPS', 'Barometer', 'Altimeter', 'GLONASS'],
    'ram' => 4,
    'camera' => 12,
    'wrapper' => 'Silver Case'
    ]);
    $iPhone = $director->buildMobile($iphoneBuilder, [
    'cpu' => 'A10',
    'gpu' => 'Apple GPU',
    'sensors' => ['UV', 'GPS', 'Barometer', 'Altimeter', 'GLONASS'],
    'ram' => 3,
    'camera' => 12,
    'wrapper' => 'Black Case'
    ]);

    echo $note3;
    echo $iPhone;