您好,登录后才能下订单哦!
PHP中工厂模式有什么用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
基本的工厂类:
<?php
class Fruit {
// 对象从工厂类返回
}
Class FruitFactory {
public static function factory() {
// 返回对象的一个新实例
return new Fruit();
}
}
// 调用工厂
$instance = FruitFactory::factory();
?>利用工厂类生产对象:
<?php
class Example
{
// The parameterized factory method
public static function factory($type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception('Driver not found');
}
}
}
// Load a MySQL Driver
$mysql = Example::factory('MySQL');
// Load an SQLite Driver
$sqlite = Example::factory('SQLite');
?>一个完整的工厂类:
下面的程序定义了一个通用的工厂类,它生产能够保存你所有操作的空对象,你可以获得一个实例,这些操作都在那个实例中了。
<?php
/**
* Generic Factory class
* This Factory will remember all operations you perform on it,
* and apply them to the object it instantiates.
*/
class FruitFactory {
private $history, $class, $constructor_args;
/**
* Create a factory of given class. Accepts extra arguments to be passed to
* class constructor.
*/
function __construct( $class ) {
$args = func_get_args();
$this->class = $class;
$this->constructor_args = array_slice( $args, 1 );
}
function __call( $method, $args ) {
$this->history[] = array(
'action' => 'call',
'method' => $method,
'args' => $args
);
}
function __set( $property, $value ) {
$this->history[] = array(
'action' => 'set',
'property' => $property,
'value' => $value
);
}
/**
* Creates an instance and performs all operations that were done on this MagicFactory
*/
function instance() {
# use Reflection to create a new instance, using the $args
$reflection_object = new ReflectionClass( $this->class );
$object = $reflection_object->newInstanceArgs( $this->constructor_args );
# Alternative method that doesn't use ReflectionClass, but doesn't support variable
# number of constructor parameters.
//$object = new $this->class();
# Repeat all remembered operations, apply to new object.
foreach( $this->history as $item ) {
if( $item['action'] == 'call' ) {
call_user_func_array( array( $object, $item['method'] ), $item['args'] );
}
if( $item['action'] == 'set' ) {
$object->{$item['property']} = $item['value'];
}
}
# Done
return $object;
}
}
class Fruit {
private $name, $color;
public $price;
function __construct( $name, $color ) {
$this->name = $name;
$this->color = $color;
}
function setName( $name ) {
$this->name = $name;
}
function introduce() {
print "Hello, this is an {$this->name} {$this->sirname}, its price is {$this->price} RMB.";
}
}
# Setup a factory
$fruit_factory = new FruitFactory('Fruit', 'Apple', 'Gonn');
$fruit_factory->setName('Apple');
$fruit_factory->price = 2;
# Get an instance
$apple = $fruit_factory->instance();
$apple->introduce();
?>工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。
工厂模式可以分为三类:
简单工厂模式(Simple Factory)
工厂方法模式(Factory Method)
抽象工厂模式(Abstract Factory)
这三种模式从上到下逐步抽象,并且更具一般性。
简单工厂模式又称静态工厂方法模式;从命名上就可以看出这个模式一定很简单。它存在的目的很简单:定义一个用于创建对象的接口。
工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。
工厂方法模式仿佛已经很完美的对对象的创建进行了包装,使得客户程序中仅仅处理抽象产品角色提供的接口。那我们是否一定要在代码中遍布工厂呢?大可不必。也许在下面情况下你可以考虑使用工厂方法模式:
当客户程序不需要知道要使用对象的创建过程。
客户程序使用的对象存在变动的可能,或者根本就不知道使用哪一个具体的对象。
关于PHP中工厂模式有什么用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。