Builder(构造者)
Posted 洞拐洞幺
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Builder(构造者)相关的知识,希望对你有一定的参考价值。
Builder(构造者)
<?php class Product { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } abstract class Builder { protected $product; final public function getProduct() { return $this->product; } public function buildProduct() { $this->product = new Product(); } } class FirstBuilder extends Builder { public function buildProduct() { parent::buildProduct(); $this->product->setName(‘The product of the first builder‘); } } class SecondBuilder extends Builder { public function buildProduct() { parent::buildProduct(); $this->product->setName(‘The product of second builder‘); } } class Factory { private $builder; public function __construct(Builder $builder) { $this->builder = $builder; $this->builder->buildProduct(); } public function getProduct() { return $this->builder->getProduct(); } } $firstDirector = new Factory(new FirstBuilder()); $secondDirector = new Factory(new SecondBuilder()); print_r($firstDirector->getProduct()->getName()); // The product of the first builder print_r($secondDirector->getProduct()->getName()); // The product of second builder
以上是关于Builder(构造者)的主要内容,如果未能解决你的问题,请参考以下文章