美文网首页
PHP设计模式之建造者模式(Builder)代码实例大全(二)

PHP设计模式之建造者模式(Builder)代码实例大全(二)

作者: 八重樱勿忘 | 来源:发表于2020-08-29 17:27 被阅读0次

    目的

    建造者是创建一个复杂对象的一部分接口。

    有时候,如果建造者对他所创建的东西拥有较好的知识储备,这个接口就可能成为一个有默认方法的抽象类(又称为适配器)。

    如果对象有复杂的继承树,那么对于建造者来说,有一个复杂继承树也是符合逻辑的。

    注意:建造者通常有一个「流式接口」,例如 PHPUnit 模拟生成器。

    例子

    • PHPUnit: 模拟生成器

    UML Diagram

    官方PHP高级学习交流社群「点击」管理整理了一些资料,BAT等一线大厂进阶知识体系备好(相关学习资料以及笔面试题)以及不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货

    代码

    • Director.php
    <?php
    
    namespace DesignPatterns\Creational\Builder;
    
    use DesignPatterns\Creational\Builder\Parts\Vehicle;
    
    /**
     * Director 类是建造者模式的一部分。 它可以实现建造者模式的接口
     * 并在构建器的帮助下构建一个复杂的对象
     *
     * 您也可以注入许多构建器而不是构建更复杂的对象
     */
    class Director
    {
        public function build(BuilderInterface $builder): Vehicle
        {
            $builder->createVehicle();
            $builder->addDoors();
            $builder->addEngine();
            $builder->addWheel();
    
            return $builder->getVehicle();
        }
    }
    
    
    • BuilderInterface.php
    <?php
    
    namespace DesignPatterns\Creational\Builder;
    
    use DesignPatterns\Creational\Builder\Parts\Vehicle;
    
    interface BuilderInterface
    {
        public function createVehicle();
    
        public function addWheel();
    
        public function addEngine();
    
        public function addDoors();
    
        public function getVehicle(): Vehicle;
    }
    
    
    • TruckBuilder.php
    <?php
    
    namespace DesignPatterns\Creational\Builder;
    
    use DesignPatterns\Creational\Builder\Parts\Vehicle;
    
    class TruckBuilder implements BuilderInterface
    {
        /**
        * @var Parts\Truck
        */
        private $truck;
    
        public function addDoors()
        {
            $this->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
    <?php
    
    namespace DesignPatterns\Creational\Builder;
    
    use DesignPatterns\Creational\Builder\Parts\Vehicle;
    
    class CarBuilder implements BuilderInterface
    {
        /**
        * @var Parts\Car
        */
        private $car;
    
        public function addDoors()
        {
            $this->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
    <?php
    
    namespace DesignPatterns\Creational\Builder\Parts;
    
    abstract class Vehicle
    {
        /**
        * @var object[]
        */
        private $data = [];
    
        /**
        * @param string $key
        * @param object $value
        */
        public function setPart($key, $value)
        {
            $this->data[$key] = $value;
        }
    }
    
    
    • Parts/Truck.php
    <?php
    
    namespace DesignPatterns\Creational\Builder\Parts;
    
    class Truck extends Vehicle
    {
    }
    
    
    • Parts/Car.php
    <?php
    
    namespace DesignPatterns\Creational\Builder\Parts;
    
    class Car extends Vehicle
    {
    }
    
    
    • Parts/Engine.php
    <?php
    
    namespace DesignPatterns\Creational\Builder\Parts;
    
    class Engine
    {
    }
    
    
    • Parts/Wheel.php
    <?php
    
    namespace DesignPatterns\Creational\Builder\Parts;
    
    class Wheel
    {
    }
    
    
    • Parts/Door.php
    <?php
    
    namespace DesignPatterns\Creational\Builder\Parts;
    
    class Door
    {
    }
    
    

    测试

    • Tests/DirectorTest.php
    <?php
    
    namespace DesignPatterns\Creational\Builder\Tests;
    
    use DesignPatterns\Creational\Builder\Parts\Car;
    use DesignPatterns\Creational\Builder\Parts\Truck;
    use DesignPatterns\Creational\Builder\TruckBuilder;
    use DesignPatterns\Creational\Builder\CarBuilder;
    use DesignPatterns\Creational\Builder\Director;
    use PHPUnit\Framework\TestCase;
    
    class DirectorTest extends TestCase
    {
        public function testCanBuildTruck()
        {
            $truckBuilder = new TruckBuilder();
            $newVehicle = (new Director())->build($truckBuilder);
    
            $this->assertInstanceOf(Truck::class, $newVehicle);
        }
    
        public function testCanBuildCar()
        {
            $carBuilder = new CarBuilder();
            $newVehicle = (new Director())->build($carBuilder);
    
            $this->assertInstanceOf(Car::class, $newVehicle);
        }
    }
    
    

    PHP 互联网架构师成长之路*「设计模式」终极指南

    PHP 互联网架构师 50K 成长指南+行业问题解决总纲(持续更新)

    面试10家公司,收获9个offer,2020年PHP 面试问题​

    ★如果喜欢我的文章,想与更多资深开发者一起交流学习的话,获取更多大厂面试相关技术咨询和指导,欢迎加入我们的群-点击此处(群号码856460874)。

    内容不错的话希望大家支持鼓励下点个赞/喜欢,欢迎一起来交流;另外如果有什么问题 建议 想看的内容可以在评论提出

    相关文章

      网友评论

          本文标题:PHP设计模式之建造者模式(Builder)代码实例大全(二)

          本文链接:https://www.haomeiwen.com/subject/tkjjsktx.html