名前が示すように、あなたが植物に原料を変換する、のようなファクトリモードは、工場のように、それが終了し、あなたは工場で何が行われたかを知る必要はありません。コードが似ている、工場における主な議論は、返品処理されたデータは、我々は工場のみ値と返された値を渡す必要性を知るために必要なものでした必要はありません。個人的に私はデザインパターンが唯一の実際の戦闘でよりよく理解することができると思い、制限された電流レベルは、我々はの交換を歓迎します
シンプルなファクトリパターン
<?php
namespace Factory\SimpleFactory;
class SimpleFactory
{
public function createProduction(): Production
{
return new Production();
}
}
class Production
{
public function getPrice(int $price)
{
return $price * 2;
}
}
class Test
{
public function __construct()
{
$factory = new SimpleFactory();
$production = $factory->createProduction();
if ($production instanceof Production) {
echo 'Nice';
}
}
}
Factory Methodパターン
それは公共の方法で主に使用制限しました
<?php
namespace Factory\SimpleFactory;
/**
* Interface FunctionFactory
* @package Factory\SimpleFactory
*/
interface FunctionFactory
{
/**
* @param array $data
* @return array
*/
public function create(array $data);
/**
* @param int $id
* @return bool
*/
public function delete(int $id);
/**
* @param array $data
* @return array
*/
public function update(array $data);
/**
* @return array
*/
public function select();
}
class ProductionRepository implements FunctionFactory
{
public function create(array $data)
{
// TODO: Implement create() method.
}
public function delete(int $id)
{
// TODO: Implement delete() method.
}
public function update(array $data)
{
// TODO: Implement update() method.
}
public function select()
{
// TODO: Implement select() method.
}
}
Abstract Factoryパターン
ファクトリメソッドAbstract Factoryパターン=パターン工場モード簡単+
<?php
namespace Factory\SimpleFactory;
/**
* Class AbstractFactory
* @package Factory\SimpleFactory
*/
class AbstractFactory
{
/**
* @param int $price
* @param int $discount
* @return PromotionPhoneProduct
*/
public function createPromotionPhoneProduct(int $price, int $discount)
{
return new PromotionPhoneProduct($price, $discount);
}
/**
* @param int $price
* @return PhoneProduct
*/
public function createPhoneProduct(int $price)
{
return new PhoneProduct($price);
}
}
/**
* Interface Product
* @package Factory\SimpleFactory
*/
interface Product
{
/**
* @return int
*/
public function calculatePrice(): int;
}
/**
* Class PhoneProduct
* @package Factory\SimpleFactory
*/
class PromotionPhoneProduct implements Product
{
/**
* @var int
*/
private $price;
/**
* @var int
*/
private $discount;
/**
* PhoneProduct constructor.
* @param int $price
* @param int $discount
*/
public function __construct(int $price, int $discount)
{
$this->price = $price;
$this->discount = $discount;
}
/**
* @return int
*/
public function calculatePrice(): int
{
return $this->price * $this->discount;
}
}
/**
* Class PhoneProduct
* @package Factory\SimpleFactory
*/
class PhoneProduct implements Product
{
/**
* @var int
*/
private $price;
/**
* PhoneProduct constructor.
* @param int $price
* @param
*/
public function __construct(int $price)
{
$this->price = $price;
}
/**
* @return int
*/
public function calculatePrice(): int
{
return $this->price;
}
}
静的ファクトリメソッド
静的メソッドは、主に、同じタイプのオブジェクトの構築に使用されています
<?php
namespace Factory\SimpleFactory;
/**
* Class StaticFactory
* @package Factory\SimpleFactory
*/
class StaticFactory
{
/**
* @param string $type
* @return NumericClass|StringClass
*/
public static function build(string $type)
{
switch ($type) {
case 'string':
return new StringClass();
break;
case 'numeric':
return new NumericClass();
default:
break;
}
}
}
/**
* Interface TypeInterface
* @package Factory\SimpleFactory
*/
interface TypeInterface
{
/**
* @return mixed
*/
public function getType();
}
/**
* Class NumericClass
* @package Factory\SimpleFactory
*/
class NumericClass implements TypeInterface
{
/**
* @return mixed|void
*/
public function getType()
{
// TODO: Implement getType() method.
}
}
/**
* Class StringClass
* @package Factory\SimpleFactory
*/
class StringClass implements TypeInterface
{
/**
* @return mixed|void
*/
public function getType()
{
// TODO: Implement getType() method.
}
}