<?php /** * */ class Product { /** * @var mixed[] */ protected static $data = array(); /** * * * @param string $key * @param mixed $value * @return void */ public static function set($key, $value) { self::$data[$key] = $value; } /** * * * @param string $key * @return mixed */ public static function get($key) { return isset(self::$data[$key]) ? self::$data[$key] : null; } /** * * * @param string $key * @return void */ final public static function removeProduct($key) { if (array_key_exists($key, self::$data)) { unset(self::$data[$key]); } } } /* * ===================================== * USING OF REGISTRY * ===================================== */ Product::set('name', 'First product'); print_r(Product::get('name')); // First product
<?php /** * */ class Factory { /** * @var Product[] */ protected static $products = array(); /** * * * @param Product $product * @return void */ public static function pushProduct(Product $product) { self::$products[$product->getId()] = $product; } /** * * * @param integer|string $id - * @return Product $product */ public static function getProduct($id) { return isset(self::$products[$id]) ? self::$products[$id] : null; } /** * * * @param integer|string $id - * @return void */ public static function removeProduct($id) { if (array_key_exists($id, self::$products)) { unset(self::$products[$id]); } } } 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
<?php /** * */ final class Product { /** * @var self */ private static $instance; /** * @var mixed */ public $a; /** * * * @return self */ public static function getInstance() { if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } /** * */ private function __construct() { } /** * */ private function __clone() { } /** * */ private function __sleep() { } /** * */ private function __wakeup() { } } /* * ===================================== * USING OF SINGLETON * ===================================== */ $firstProduct = Product::getInstance(); $secondProduct = Product::getInstance(); $firstProduct->a = 1; $secondProduct->a = 2; print_r($firstProduct->a); // 2 print_r($secondProduct->a); // 2
<?php /** * */ abstract class FactoryAbstract { /** * @var array */ protected static $instances = array(); /** * , * * @return static */ public static function getInstance() { $className = static::getClassName(); if (!(self::$instances[$className] instanceof $className)) { self::$instances[$className] = new $className(); } return self::$instances[$className]; } /** * , * * @return void */ public static function removeInstance() { $className = static::getClassName(); if (array_key_exists($className, self::$instances)) { unset(self::$instances[$className]); } } /** * * * @return string */ final protected static function getClassName() { return get_called_class(); } /** * */ protected function __construct() { } /** * */ final protected function __clone() { } /** * */ final protected function __sleep() { } /** * */ final protected function __wakeup() { } } /** * */ abstract class Factory extends FactoryAbstract { /** * , * * @return static */ final public static function getInstance() { return parent::getInstance(); } /** * , * * @return void */ final public static function removeInstance() { parent::removeInstance(); } } /* * ===================================== * USING OF MULTITON * ===================================== */ /** * */ class FirstProduct extends Factory { public $a = []; } /** * */ class SecondProduct extends FirstProduct { } // FirstProduct::getInstance()->a[] = 1; SecondProduct::getInstance()->a[] = 2; FirstProduct::getInstance()->a[] = 3; SecondProduct::getInstance()->a[] = 4; print_r(FirstProduct::getInstance()->a); // array(1, 3) print_r(SecondProduct::getInstance()->a); // array(2, 4)
<?php /** * */ abstract class RegistryFactory extends FactoryAbstract { /** * , * * @param integer|string $id - * @return static */ final public static function getInstance($id) { $className = static::getClassName(); if (isset(self::$instances[$className])) { if (!(self::$instances[$className][$id] instanceof $className)) { self::$instances[$className][$id] = new $className($id); } } else { self::$instances[$className] = [ $id => new $className($id), ]; } return self::$instances[$className][$id]; } /** * , * * @param integer|string $id - . , * @return void */ final public static function removeInstance($id = null) { $className = static::getClassName(); if (isset(self::$instances[$className])) { if (is_null($id)) { unset(self::$instances[$className]); } else { if (isset(self::$instances[$className][$id])) { unset(self::$instances[$className][$id]); } if (empty(self::$instances[$className])) { unset(self::$instances[$className]); } } } } protected function __construct($id) { } } /* * ===================================== * USING OF MULTITON * ===================================== */ /** * */ class FirstFactory extends RegistryFactory { public $a = []; } /** * */ class SecondFactory extends FirstFactory { } // 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)
<?php /** * */ interface Factory { /** * * * @return Product */ public function getProduct(); } /** * */ interface Product { /** * * * @return string */ public function getName(); } /** * */ class FirstFactory implements Factory { /** * * * @return Product */ public function getProduct() { return new FirstProduct(); } } /** * */ class SecondFactory implements Factory { /** * * * @return Product */ public function getProduct() { return new SecondProduct(); } } /** * */ class FirstProduct implements Product { /** * * * @return string */ public function getName() { return 'The first product'; } } /** * */ class SecondProduct implements Product { /** * * * @return string */ public function getName() { return 'Second product'; } } /* * ===================================== * USING OF FACTORY METHOD * ===================================== */ $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
<?php /** * - */ class Config { public static $factory = 1; } /** * - */ interface Product { /** * * * @return string */ public function getName(); } /** * */ abstract class AbstractFactory { /** * * * @return AbstractFactory - * @throws Exception */ public static function getFactory() { switch (Config::$factory) { case 1: return new FirstFactory(); case 2: return new SecondFactory(); } throw new Exception('Bad config'); } /** * * * @return Product */ abstract public function getProduct(); } /* * ===================================== * FIRST FAMILY * ===================================== */ class FirstFactory extends AbstractFactory { /** * * * @return Product */ public function getProduct() { return new FirstProduct(); } } /** * */ class FirstProduct implements Product { /** * * * @return string */ public function getName() { return 'The product from the first factory'; } } /* * ===================================== * SECOND FAMILY * ===================================== */ class SecondFactory extends AbstractFactory { /** * * * @return Product */ public function getProduct() { return new SecondProduct(); } } /** * */ class SecondProduct implements Product { /** * * * @return string */ public function getName() { return 'The product from second factory'; } } /* * ===================================== * USING OF ABSTRACT FACTORY * ===================================== */ $firstProduct = AbstractFactory::getFactory()->getProduct(); Config::$factory = 2; $secondProduct = AbstractFactory::getFactory()->getProduct(); print_r($firstProduct->getName()); // The first product from the first factory print_r($secondProduct->getName()); // Second product from second factory
<?php /** * - */ interface Product { /** * * * @return string */ public function getName(); } class Factory { /** * @var Product */ protected $firstProduct; /** * @var Product */ protected $secondProduct; /** * * * @return Product */ public function getFirstProduct() { if (!$this->firstProduct) { $this->firstProduct = new FirstProduct(); } return $this->firstProduct; } /** * * * @return Product */ public function getSecondProduct() { if (!$this->secondProduct) { $this->secondProduct = new SecondProduct(); } return $this->secondProduct; } } /** * */ class FirstProduct implements Product { /** * * * @return string */ public function getName() { return 'The first product'; } } /** * */ class SecondProduct implements Product { /** * * * @return string */ public function getName() { return 'Second product'; } } /* * ===================================== * USING OF LAZY INITIALIZATION * ===================================== */ $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 { } /** * - */ class Factory { /** * @var Product */ private $product; /** * @param Product $product */ public function __construct(Product $product) { $this->product = $product; } /** * * * @return Product */ public function getProduct() { return clone $this->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
<?php /** * - */ class Product { /** * @var string */ private $name; /** * @param string $name */ public function setName($name) { $this->name = $name; } /** * @return string */ public function getName() { return $this->name; } } /** * - */ class Factory { /** * @var Builder */ private $builder; /** * @param Builder $builder */ public function __construct(Builder $builder) { $this->builder = $builder; $this->builder->buildProduct(); } /** * * * @return Product */ public function getProduct() { return $this->builder->getProduct(); } } /** * - */ abstract class Builder { /** * @var Product */ protected $product; /** * * * @return Product */ final public function getProduct() { return $this->product; } /** * * * @return void */ public function buildProduct() { $this->product = new Product(); } } /** * */ class FirstBuilder extends Builder { /** * * * @return void */ public function buildProduct() { parent::buildProduct(); $this->product->setName('The product of the first builder'); } } /** * */ class SecondBuilder extends Builder { /** * * * @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
Source: https://habr.com/ru/post/214285/
All Articles