php 工厂模式用在哪,PHP 工厂模式介绍

工厂模式,顾名思义,如同工厂一样,你把原材料放入工厂中,出来的是成品,而你并不需要知道工厂里做了什么。代码中也类似,把主要参数放入一个工厂里,返回的是处理好的数据,我们并不需要工厂里做了什么,只需要知道需要传入的值和返回的值。个人认为设计模式只能在实战中更好的理解,当前水平有限,欢迎大家交流

简单工厂模式

7c0c1938297c0c35fbecbac6c260c6bc.png

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';

}

}

}

工厂方法模式

主要用于限制类的公用方法

4469072ccf33d7789f7225634dc48690.png

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.

}

}

抽象工厂模式

抽象工厂模式 = 工厂方法模式+简易工厂模式

0adb0bd2656bf09c36351fcd966e8670.png

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;

}

}

静态工厂方法

静态方法主要用于构建相同类型的对象

b2e1599effb4915cafc5e755db31624c.png

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.

}

}

标签:function,return,int,price,模式,工厂,PHP

来源: https://www.cnblogs.com/it-abel/p/11031845.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值