PHP 工厂模式介绍
工厂模式,顾名思义,如同工厂一样,你把原材料放入工厂中,出来的是成品,而你并不需要知道工厂里做了什么,工厂模式主要用于解耦。个人认为设计模式只能在实战中更好的理解,当前水平有限,欢迎大家交流
简单工厂模式
把对象的创建和使用的过程分开,比如: ClassA 调用 ClassB,那么 ClassA 只调用ClassB 的方法,至于实例化 ClassB 则在工厂内实现。这样既减少了代码的重复使用,也方便对 ClassB de 后期维护。如果 ClassB 实例化过程很复杂,使用简单工厂模式就会发现外部无需关注复杂的实例化,只管调用 ClassB 的方法即可,减少错误

<?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';
}
}
}
工厂方法模式
主要用于限制类的公用方法
<?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.
}
}
抽象工厂模式
抽象工厂模式 = 工厂方法模式+简易工厂模式
<?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.
}
}
四种工厂模式的优缺点
待补充
PHP 工厂模式介绍的更多相关文章
- Java设计模式(三) 抽象工厂模式
原创文章,同步发自作者个人博客,转载请注明出处 http://www.jasongj.com/design_pattern/abstract_factory/ 抽象工厂模式解决的问题 上文<工厂 ...
- java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...
- 设计模式C++实现(1)——工厂模式
该文章转载自: http://blog.csdn.net/wuzhekai1985 软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装 ...
- Java 工厂模式(一)— 抽象工厂(Abstract Factory)模式
一.抽象工厂模式介绍: 1.什么是抽象工厂模式: 抽象工厂模式是所有形态的工厂模式中最为抽象和最具有一般性的一种形态,抽象工厂模式向客户端提供一个接口,使得客户端在不知道具体产品的情类型的情况下,创建 ...
- 浅析JavaScript工厂模式
这里主要介绍两种工厂模式,第一种“简单工厂模式”,第二种“工厂方法模式” 简单工厂模式 1.定义 由一个工厂对象决定对象创建某一种产品对象的的实例.主要用来创建同一类对象. 2.具体需求 现在有一个登 ...
- Java设计模式之【工厂模式】(简单工厂模式,工厂方法模式,抽象工厂模式)
Java设计模式之[工厂模式](简单工厂模式,工厂方法模式,抽象工厂模式) 工厂模式出现的原因 在java中,创建一个对象最简单的方法就是使用new关键字.但在一些复杂的业务逻辑中,创建一个对象不只需 ...
- C#设计模式(3)——抽象工厂模式
1.抽象工厂模式介绍 上一篇我们了解了工厂模式,知道工厂模式可以解决简单工厂的缺陷(简单工厂添加新产品时要修改工厂类,不符合开闭原则),但是简单工厂和工厂模式都是只生产一种产品(前边的简单工厂和工厂都 ...
- C#设计模式(2)——工厂模式
1.工厂模式介绍 上一篇我们知道了简单工厂的缺点是:当我们添加一个新的产品时需要修改工厂类,这样就违背了开闭原则.工厂模式就是为了解决这一缺陷而出现的,解决的方法是把创建具体实例的任务放在了工厂的子类 ...
- JavaScript设计模式—工厂模式
工厂模式介绍 将new操作符单独进行封装,遇到new时,就要考虑是否该使用工厂模式 举一个生活当中的示例: 你要去购买汉堡,直接点餐,取餐,不会自己动手做,商店要“封装” 做汉堡的工作,做好直接给购买 ...
随机推荐
- 在CMD下运用管理员权限
方法一:鼠标右键 这个方法比较比较普通,点开开始找到cmd,右击鼠标“以管理员身份运行(A)”这样调用就是管理员的权限: 方法二:快捷模式 在点开win+R后,选择“以管理员身份运行”,然后确定:可以 ...
- java多线程,如何防止脏读数据
多线程容易“非线程安全”的情况,是由于用了全局变量,而又没有很好的控制起情况.所以无论做什么程序,谨慎使用全局变量 "非线程安全"其实会在多个线程对同一个对象中的实例变量进行并发访 ...
- zblog去除文章页作者信息
不想让zblog文章页显示作者信息怎么办? 1. 找到文章页模板文件:/zb_users/theme/default/template/post-single.php,删除相关代码 <span ...
- EPSG:4326
简单说,"EPSG:4326"指的就是WGS84坐标系 参考 http://blog.csdn.net/cloverwindy/article/details/8663968 AU ...
- mysql用户权限操作
mysql用户权限操作1.创建用户mysql -urootcreate database zabbix default charset utf8;grant all on zabbix.* to za ...
- centOS7虚拟机连接大网
1.启动vm服务 如果遇到无法启动时,需要还原vm默认配置解决 2.更改vm设置为NAT模式 3.centOS开启DHCP
- C#之MVC3继续整理问题
1.注释验证[EmailAddress(ErrorMessage = "×")],用的MVC3框架,此处报错,找不到类“EmailAddress”,看到原文有using Syste ...
- HDU 1712 ACboy needs your help AC男需要你的帮助 (分组的背包)
分组背包问题:有N件物品和一个容量为V的背包.第i件物品的体积是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品装入背包可使这些物品的体积总和不超过背 ...
- leetcode——2
1. 题目 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi ...
- 如何查看CRM WebUI,C4C和Hybris里的页面技术信息
CRM 在WebClient UI页面上按F2,就能看到页面的技术信息, 可以找到当前页面是哪一个BSP component实现的: C4C 在浏览器url里添加debugMode=true,然后按住 ...


