当前位置: 首页>>代码示例>>PHP>>正文


PHP Factory::getProduct方法代码示例

本文整理汇总了PHP中Factory::getProduct方法的典型用法代码示例。如果您正苦于以下问题:PHP Factory::getProduct方法的具体用法?PHP Factory::getProduct怎么用?PHP Factory::getProduct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Factory的用法示例。


在下文中一共展示了Factory::getProduct方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: getProduct

    /**
     * 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
开发者ID:Bazitron,项目名称:php-design-patterns,代码行数:31,代码来源:example-1.php

示例2: __construct

    }
}
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
开发者ID:Bazitron,项目名称:php-design-patterns,代码行数:31,代码来源:example-1.php

示例3: getName

    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'));
开发者ID:rainbowzl7,项目名称:design-patterns-in-php,代码行数:30,代码来源:ObjectPool.php

示例4: buildProduct

        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
开发者ID:Bazitron,项目名称:php-design-patterns,代码行数:31,代码来源:example-1.php

示例5: 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 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());
开发者ID:rainbowzl7,项目名称:design-patterns-in-php,代码行数:30,代码来源:Builder.php


注:本文中的Factory::getProduct方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。