The complete collection of builder code examples of PHP design pattern (2)

Time:2021-3-6

objective

The builder is part of the interface that creates a complex object.

Sometimes, if the builder has a good knowledge of what he creates, the interface may become an abstract class (also known as an adapter) with default methods.

If an object has a complex inheritance tree, it is logical for the builder to have a complex inheritance tree.

Note: the builder usually has a “streaming interface”, such as phpunit simulator.

example

  • Phpunit: simulation generator

UML Diagram

Official PHP advanced learning exchange community “click”The management has sorted out some materials, bat and other advanced knowledge systems are ready (relevant learning materials and written questions), and not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, spool, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginx and other advanced knowledge points Advanced dry goods

code

  • Director.php
createVehicle();
        $builder->addDoors();
        $builder->addEngine();
        $builder->addWheel();

        return $builder->getVehicle();
    }
}
  • BuilderInterface.php
  • TruckBuilder.php
truck->setPart('rightDoor', new Parts\Door());
        $this->truck->setPart('leftDoor', new Parts\Door());
    }

    public function addEngine()
    {
        $this->truck->setPart('truckEngine', new Parts\Engine());
    }

    public function addWheel()
    {
        $this->truck->setPart('wheel1', new Parts\Wheel());
        $this->truck->setPart('wheel2', new Parts\Wheel());
        $this->truck->setPart('wheel3', new Parts\Wheel());
        $this->truck->setPart('wheel4', new Parts\Wheel());
        $this->truck->setPart('wheel5', new Parts\Wheel());
        $this->truck->setPart('wheel6', new Parts\Wheel());
    }

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

    public function getVehicle(): Vehicle
    {
        return $this->truck;
    }
}
  • CarBuilder.php
car->setPart('rightDoor', new Parts\Door());
        $this->car->setPart('leftDoor', new Parts\Door());
        $this->car->setPart('trunkLid', new Parts\Door());
    }

    public function addEngine()
    {
        $this->car->setPart('engine', new Parts\Engine());
    }

    public function addWheel()
    {
        $this->car->setPart('wheelLF', new Parts\Wheel());
        $this->car->setPart('wheelRF', new Parts\Wheel());
        $this->car->setPart('wheelLR', new Parts\Wheel());
        $this->car->setPart('wheelRR', new Parts\Wheel());
    }

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

    public function getVehicle(): Vehicle
    {
        return $this->car;
    }
}
  • Parts/Vehicle.php
data[$key] = $value;
    }
}
  • Parts/Truck.php
  • Parts/Car.php
  • Parts/Engine.php
  • Parts/Wheel.php
  • Parts/Door.php

test

  • Tests/DirectorTest.php
build($truckBuilder);

        $this->assertInstanceOf(Truck::class, $newVehicle);
    }

    public function testCanBuildCar()
    {
        $carBuilder = new CarBuilder();
        $newVehicle = (new Director())->build($carBuilder);

        $this->assertInstanceOf(Car::class, $newVehicle);
    }
}

PHP Internet architect’s growth path * “design patterns” Ultimate Guide

PHP Internet architect 50K growth Guide + general outline of industry problem solving (continuous update)

Interview 10 companies, get 9 offers, PHP interview questions in 2020

If you like my article and want to exchange and study with more senior developers, you can get more technical consultation and guidance related to the interview of large factories. Welcome to join usOur group – click here(group number 856460874).

If the content is good, I hope you can support and encourage me to like it, and welcome to communicate with me. In addition, if you have any questions, please put forward the content you want to see in the comments