Ejemplo n.º 1
0
    /**
     * Returns new instance of the product by cloning
     *
     * @return Product
     */
    public function getProduct()
    {
        return clone $this->product;
    }
}
/**
 * The product
 */
class SomeProduct implements Product
{
    public $name;
}
/*
 * =====================================
 *          USING OF PROTOTYPE
 * =====================================
 */
$prototypeFactory = new Factory(new SomeProduct());
$firstProduct = $prototypeFactory->getProduct();
$firstProduct->name = 'The first product';
$secondProduct = $prototypeFactory->getProduct();
$secondProduct->name = 'Second product';
print_r($firstProduct->name);
// The first product
print_r($secondProduct->name);
// Second product
Ejemplo n.º 2
0
    }
}
class Product
{
    /**
     * @var integer|string
     */
    protected $id;
    public function __construct($id)
    {
        $this->id = $id;
    }
    /**
     * @return integer|string
     */
    public function getId()
    {
        return $this->id;
    }
}
/*
 * =====================================
 *         USING OF OBJECT POOL
 * =====================================
 */
Factory::pushProduct(new Product('first'));
Factory::pushProduct(new Product('second'));
print_r(Factory::getProduct('first')->getId());
// first
print_r(Factory::getProduct('second')->getId());
// second
    public function getName()
    {
        return $this->name;
    }
}
class Factory
{
    protected static $products = array();
    public static function pushProduct(Product $product)
    {
        self::$products[$product->getId()] = $product;
    }
    public static function getProduct($id)
    {
        return isset(self::$products[$id]) ? self::$products[$id] : null;
    }
    public static function removeProduct($id)
    {
        if (array_key_exists($id, self::$products)) {
            unset(self::$products[$id]);
        }
    }
}
Factory::pushProduct(new Product('first', 'first_product'));
Factory::pushProduct(new Product('second', 'second_product'));
var_dump(Factory::getProduct('first')->getName());
var_dump(Factory::getProduct('second')->getName());
var_dump(Factory::getProduct('second'));
Factory::removeProduct('second');
var_dump(Factory::getProduct('second'));
Ejemplo n.º 4
0
        parent::buildProduct();
        $this->product->setName('The product of the first builder');
    }
}
/**
 * Second builder
 */
class SecondBuilder extends Builder
{
    /**
     * Creates the product
     *
     * @return void
     */
    public function buildProduct()
    {
        parent::buildProduct();
        $this->product->setName('The product of second builder');
    }
}
/*
 * =====================================
 *            USING OF BUILDER
 * =====================================
 */
$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
Ejemplo n.º 5
0
    {
        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 the 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());
var_dump($firstDirector->getProduct()->getName());
var_dump($secondDirector->getProduct()->getName());