示例#1
0
    }
}
class SecondFactory implements Factory
{
    public function getProduct()
    {
        return new SecondProduct();
    }
}
class FirstProduct implements Product
{
    public function getName()
    {
        return 'The first product';
    }
}
class SecondProduct implements Product
{
    public function getName()
    {
        return 'Second product';
    }
}
$factory = new FirstFactory();
$firstProduct = $factory->getProduct();
$factory = new SecondFactory();
$secondProduct = $factory->getProduct();
print_r($firstProduct->getName());
// The first product
print_r($secondProduct->getName());
// Second product
 */
/**
 * First object pool
 */
class FirstFactory extends RegistryFactory
{
    public $a = [];
}
/**
 * Second instance pool
 */
class SecondFactory extends FirstFactory
{
}
// Filling property of singletons
FirstFactory::getInstance('FirstProduct')->a[] = 1;
FirstFactory::getInstance('SecondProduct')->a[] = 2;
SecondFactory::getInstance('FirstProduct')->a[] = 3;
SecondFactory::getInstance('SecondProduct')->a[] = 4;
FirstFactory::getInstance('FirstProduct')->a[] = 5;
FirstFactory::getInstance('SecondProduct')->a[] = 6;
SecondFactory::getInstance('FirstProduct')->a[] = 7;
SecondFactory::getInstance('SecondProduct')->a[] = 8;
print_r(FirstFactory::getInstance('FirstProduct')->a);
// array(1, 5)
print_r(FirstFactory::getInstance('SecondProduct')->a);
// array(2, 6)
print_r(SecondFactory::getInstance('FirstProduct')->a);
// array(3, 7)
print_r(SecondFactory::getInstance('SecondProduct')->a);
// array(4, 8)