面向对象编程中,工厂模式是我们最常用的实例化对象模式,工厂类就是一个专门用来创建其它对象的类,工厂类在多态性编程实践中是非常重要的。它允许动态替换类,修改配置,会使应用程序更加灵活。掌握工厂模式对web开发是必不可少的,它会给你的系统带来更大的可扩展性和尽量少的修改量。

工厂模式通常用来返回类似接口的不同的类,工厂的一种常见用法就是创建多态的提供者。

通常工厂模式有一个关键的构造,即一般被命名为factory的静态方法。这个静态方法可以接受任意数量的参数,并且必须返回一个对象。

一个非常贴近生活的例子来告诉你什么是工厂模式

但是工厂模式真的是个累赘吗?其实并不是!他能够作为一种设计模式流传至今,一定是有他的道理的!只不过我们看到的例子只能说明工厂模式是什么,并不能很好说明工厂模式的优点,所以我们学会后并不知道为什么要使用工厂模式,以及什么时候应该去使用工厂模式!

其实工厂模式在我们的现实生活中非常常见,下面我举个生活中的例子,大家应该就能明白工厂模式的用处在哪里了!

麦当劳大家都吃过吧?我们去点餐的时候,我们可以点一个汉堡,一杯可乐,一个薯条。我们还可以点一杯可乐,一个薯条。点完之后点餐员会问我们一句还要别的吗?你说不要了! 然后你的这一份餐就点完了,可以给钱了。咦,我们发现这是一个建造者模式(builder pattern)啊!

(ps:这确实是突然发现的,之前写建造者模式那篇文章的时候并没有想到这个例子)

基本的工厂类:

<?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)

这三种模式从上到下逐步抽象,并且更具一般性。

简单工厂模式又称静态工厂方法模式;从命名上就可以看出这个模式一定很简单。它存在的目的很简单:定义一个用于创建对象的接口。

工厂方法模式去掉了简单工厂模式中工厂方法的静态属性,使得它可以被子类继承。这样在简单工厂模式里集中在工厂方法上的压力可以由工厂方法模式里不同的工厂子类来分担。

工厂方法模式仿佛已经很完美的对对象的创建进行了包装,使得客户程序中仅仅处理抽象产品角色提供的接口。那我们是否一定要在代码中遍布工厂呢?大可不必。也许在下面情况下你可以考虑使用工厂方法模式:

  • 当客户程序不需要知道要使用对象的创建过程。
  • 客户程序使用的对象存在变动的可能,或者根本就不知道使用哪一个具体的对象。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对www.887551.com的支持。如果你想了解更多相关内容请查看下面相关链接