Design Patterns in OOP PHP Builder Design Pattern

Design Patterns in OOP PHP Builder Design Pattern

 Builder Design Pattern

Is to create complex object with separation of the way object was created and it creation Sperate construction and representation that means we can create different objects from the same construction way
 

Example 

we want to create two different types of cars with the same manufacture process but with different representation


This will be include as following:

  1. create  car builder interface that have all methods to be implemented .

  2. create model classes responsible for inserting data in each builder class

  3. create builder classes that implements car builder interface each class has its own implementation for method

  4. create class producer that do the real implementation its constructor will take the car builder interface as parameter and in the runtime it will actually takes the proper class implementation 









interface Builder 

 CarBuilderInterface.php

<?php
namespace Creational\Builder;

interface CarBuilderInterface{

    public function createCar();
    public function addEngine();
    public function addBody();
    public function addDoors();
    public function addWheels();
    public function getCar();
}


?>







models

BmwCar.php

<?php
namespace Creational\Builder\Models;

class BmwCar{

   private $data;

   public function setParts($name, $value){
    $this->data[$name]=$value;
   }
}


?>
BenzCar

<?php
namespace Creational\Builder\Models;

class BenzCar{

   private $data;

   public function setParts($name, $value){
    $this->data[$name]=$value;
   }
}


?>


builder classes 

<?php
namespace Creational\Builder;
use Creational\Builder\Models\BenzCar;

class BenzCarBuilder implements CarBuilderInterface{

    private $type;
    public function createCar(){
        $this->type =new BenzCar();
    }
    public function addEngine(){
       return $this->type->setParts('Engine','benz-engine') ;
    }
    public function addBody(){
        return $this->type->setParts('Body','benz-body') ;

    }
    public function addDoors(){
        return $this->type->setParts('Doors','benz-doors') ;

    }
    public function addWheels(){
        return $this->type->setParts('Wheels','benz-heels') ;
   
    }
    public function getCar(){
        return $this->type;

    }
}


?>

<?php
namespace Creational\Builder;
use Creational\Builder\Models\BmwCar;

class BmwCarBuilder implements CarBuilderInterface {

    private $type;
    public function createCar(){
        $this->type =new BmwCar();
    }
    public function addEngine(){
       return $this->type->setParts('Engine','bm-engine') ;
    }
    public function addBody(){
        return $this->type->setParts('Body','bm-body') ;

    }
    public function addDoors(){
        return $this->type->setParts('Doors','bm-doors') ;

    }
    public function addWheels(){
        return $this->type->setParts('Wheels','bm-wheels') ;
   
    }
    public function getCar(){
        return $this->type;

    }
}


?>

CarProducer

<?php

namespace Creational\Builder;

class CarProducer{

private $Builder;

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

public function ProducerCar(){
$this->Builder->createCar();
$this->Builder->addBody();
$this->Builder->addDoors();
$this->Builder->addEngine();
$this->Builder->addWheels();
return $this->Builder->getCar();
   
}
}

?>

test cases

<?php
namespace Tests;

use PHPUnit\Framework\TestCase;
use Creational\Builder\CarProducer;
use Creational\Builder\BmwCarBuilder;
use Creational\Builder\BenzCarBuilder;
use Creational\Builder\Models\BenzCar;
use Creational\Builder\Models\BmwCar;

class BuilderClassTest extends TestCase
{
/**
 *@test
 */
    public function testCanProduceBmWCar(){
        //here we create  class instance  BmwCarBuilder
        $BmBuilder = new BmwCarBuilder();
        //here we make a CarProducer instance and pass BmwCarBuilder instance
        $carProducer =new CarProducer($BmBuilder);
                // use CarProducer instance to produce a ct

        $myCar = $carProducer->ProducerCar();
        $this->assertInstanceOf(BmWCar::Class,$myCar);


    }
    public function testCanProduceBenzCar(){
        //here we create  class instance  BenzCarBuilder
        $Builder = new BenzCarBuilder();
        //here we make a CarProducer instance and pass BenzCarBuilder instance
        $carProducer =new CarProducer($Builder );
        // use CarProducer instance to produce a ct
        $myCar = $carProducer->ProducerCar();

        $this->assertInstanceOf(BenzCar::Class,$myCar);


    }
}


?>

$ ./vendor/bin/phpunit tests/BuilderClassTest PHPUnit 7.5.20 by Sebastian Bergmann and contributors. Runtime: PHP 7.4.28 Configuration: C:\xampp7\htdocs\desginPattern\phpunit.xml EE 2 / 2 (100%) Time: 58 ms, Memory: 4.00 MB T $ ./vendor/bin/phpunit tests/BuilderClassTest PHPUnit 7.5.20 by Sebastian Bergmann and contributors. Runtime: PHP 7.4.28 Configuration: C:\xampp7\htdocs\desginPattern\phpunit.xml .. 2 / 2 (100%) Time: 58 ms, Memory: 4.00 MB OK (2 tests, 2 assertions)


source


https://www.youtube.com/watch?v=PnMIbeLgdPI&list=PLdYYj2XLw5BnpInmR103TyVwFd_CLI6IS&index=3
https://www.youtube.com/watch?v=EQssT5f6NCU&list=PLdYYj2XLw5BnpInmR103TyVwFd_CLI6IS&index=4

Comments

Popular posts from this blog

ES6 : 5 Prevent Object Mutation

ES6: 1 Explore Differences Between the var and let Keywords

CSS Grid: Reduce Repetition Using the repeat Function