Tiếp nối loại bài viết về DESIGN PATTERNS hôm nay chúng ta cùng tìm hiểu vê Creational-Builder pattern trong PHP các bạn nhé

GIới thiệu Builder pattern

Builder pattern cho phép lập trình viên tạo ra những đối tượng phức tạp nhưng chỉ cần thông qua các câu lệnh đơn giản để tác động nên các thuộc tính của nó.

Builder pattern có lợi khi:

  • Muốn thay đổi thiết kế cho việc lồng nhau của các hàm khởi tạo.
  • Cần tạo ra một đối tượng phức tạp, một đối tượng mà thuật toán để tạo tạo lập các thuộc tính là độc lập đối với các thuộc tính khác.

Builder pattern có độ hot chẳng kém abstract factory pattern cùng đc các framework lớn sử dụng như: Laravel, Symfony,...

Ví Dụ minh họa Builder pattern trong PHP

Chúng ta sẽ thiết kế builder pattern theo UML sau:

Builder pattern UML

Code:

<?php
class Directer
{
    public function build(BuilderInterFace $build)
    {
        $build->createVehicle();
        $build->addDoors();
        $build->addEngine();
        $build->addWheel();
        return $build->getVehicle();
    }
}

interface BuilderInterFace
{
    public function createVehicle();
    public function addWheel();
    public function addEngine();
    public function addDoors();
    public function getVehicle();
}

class TruckBuilder implements BuilderInterFace
{
    /**
     * @var Object
     */
    private $truck;
    public function addDoors()
    {
        $this->truck->setPart('rightDoor', new Door());
        $this->truck->setPart('lefttDoor', new Door());
    }
    public function addEngine()
    {
        $this->truck->setPart('truckEngine', new Engine);
    }
    public function addWheel()
    {
        $this->truck->setPart('wheel1', new Wheel());
        $this->truck->setPart('wheel2', new Wheel());
        $this->truck->setPart('wheel3', new Wheel());
        $this->truck->setPart('wheel4', new Wheel());

    }
    public function createVehicle()
    {
        $this->truck = new Truck();
    }
}

class CarBuilder implements BuilderInterFace
{
    /**
     * @var Object
     */
    private $car;
    public function addDoors()
    {
        $this->car->setPart('rightDoor', new Door());
        $this->car->setPart('lefttDoor', new Door());
        $this->car->setPart('trunkLid', new Door());
    }
    public function addEngine()
    {
        $this->car->setPart('Engine', new Engine);
    }
    public function addWheel()
    {
        $this->car->setPart('wheelLF', new Wheel());
        $this->car->setPart('wheelRF', new Wheel());
        $this->car->setPart('wheelLR', new Wheel());
        $this->car->setPart('wheelRR', new Wheel());

    }
    public function createVehicle()
    {
        $this->car = new Car();
    }
    public function getVehicle()
    {
        return $this->car;
    }
}

abstract class Vehicle{
    /**
     * @var [object]
     */
    private $data = [];
    /**
     * Set Data
     * @param string $key 
     * @param object $val 
     */
    public function setPart($key, $val)
    {
        $this->data[$key] = $val;
    }
    public function getPart()
    {
        return $this->data;
    }
}

class Truck extends Vehicle
{
    //code
}

class Car extends Vehicle
{
    //code
}

class Door
{
    //code
}

class Engine
{
    //code
}

class Wheel
{
    //code
}

//Khởi tạo
$car = new CarBuilder();
$object = (new Directer())->build($car);
var_dump($object);

Mặc dù thiết kế hết sức phức tạp, nhưng khi chúng ta tác động vào các thuộc tính của nó lại rất đơn giản.

Viết câu trả lời

Drop Images

0 Bình luận