Object pool(对象池)
对象池可以用于构造并且存放一系列的对象并在需要时获取调用:
<?php
class Product {
protected $id;
public function __construct($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
}
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'));
Factory::pushProduct(new Product('second'));
print_r(Factory::getProduct('first')->getId());
// first
print_r(Factory::getProduct('second')->getId());
// second
<?php
class Product {
protected $id;
public function __construct($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
}
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'));
Factory::pushProduct(new Product('second'));
print_r(Factory::getProduct('first')->getId());
// first
print_r(Factory::getProduct('second')->getId());
// second
Lazy Initialization(延迟初始化)
对于某个变量的延迟初始化也是常常被用到的,对于一个类而言往往并不知道它的哪个功能会被用到,而部分功能往往是仅仅被需要使用一次。
<?php
interface Product {
public function getName();
}
class Factory {
protected $firstProduct;
protected $secondProduct;
public function getFirstProduct() {
if (!$this->firstProduct) {
$this->firstProduct = new FirstProduct();
}
return $this->firstProduct;
}
public function getSecondProduct() {
if (!$this->secondProduct) {
$this->secondProduct = new SecondProduct();
}
return $this->secondProduct;
}
}
class FirstProduct implements Product {
public function getName() {
return 'The first product';
}
}
class SecondProduct implements Product {
public function getName() {
return 'Second product';
}
}
$factory = new Factory();
print_r($factory->getFirstProduct()->getName());
// The first product
print_r($factory->getSecondProduct()->getName());
// Second product
print_r($factory->getFirstProduct()->getName());
// The first product
<?php
interface Product {
public function getName();
}
class Factory {
protected $firstProduct;
protected $secondProduct;
public function getFirstProduct() {
if (!$this->firstProduct) {
$this->firstProduct = new FirstProduct();
}
return $this->firstProduct;
}
public function getSecondProduct() {
if (!$this->secondProduct) {
$this->secondProduct = new SecondProduct();
}
return $this->secondProduct;
}
}
class FirstProduct implements Product {
public function getName() {
return 'The first product';
}
}
class SecondProduct implements Product {
public function getName() {
return 'Second product';
}
}
$factory = new Factory();
print_r($factory->getFirstProduct()->getName());
// The first product
print_r($factory->getSecondProduct()->getName());
// Second product
print_r($factory->getFirstProduct()->getName());
// The first product
Prototype(原型模式)
有些时候,部分对象需要被初始化多次。而特别是在如果初始化需要耗费大量时间与资源的时候进行预初始化并且存储下这些对象。
<?php
interface Product {
}
class Factory {
private $product;
public function __construct(Product $product) {
$this->product = $product;
}
public function getProduct() {
return clone $this->product;
}
}
class SomeProduct implements Product {
public $name;
}
$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
<?php
interface Product {
}
class Factory {
private $product;
public function __construct(Product $product) {
$this->product = $product;
}
public function getProduct() {
return clone $this->product;
}
}
class SomeProduct implements Product {
public $name;
}
$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