2012-12-14 3 views
0

팩토리 패턴의 개념을 이해하면 같은 템플리트의 것을 뱉어 낼 수 있습니다. 그래서 제가 공장 클래스의 사과를 주면 많은 사과를 되 찾을 수있을 것입니다. 새로운 사과를 이제는 인스턴스화하지 않아도됩니다.필수 인수가있는 클래스에 대한 팩토리 작성하기

사과가 필수 인수 인 경우 또는 시드, 단계 및 잎의 필수 인수가 여러 개인 경우 어떻게해야합니까? 여기에 공장 패턴을 어떻게 사용합니까?

입니다 어떻게이 인스턴스를 공장 패턴을 사용하십시오

$apple = new Apple($seed, $stem, $leaf); 
+2

글쎄, (또는 다른 공장)'시드','스템'과'떠나기'를 인스턴스화합니까? 공장은 결코 하나의 클래스를 인스턴스화하는 데 제한되지 않습니다 ... – Wrikken

답변

0

이 울부 짖는 소리 같이 완벽하게 문서화 할 수있는 방법입니다. 이 공장은 작동합니다.

if(isset(self::$_dependencies[$class]['params'])){ 
     $new_class = new $class(implode(', ', self::$_dependencies[$class]['params'])); 
     return $new_class; 
    }else{ 
     $new_class = new $class(); 
     return $new_class; 
    } 

우리는 어떤 PARAMS의 배열을 확인하고 몇 가지가있는 경우 우리는 내파과에 의해 함께 접착제 :이 클래스의 아웃

<?php 
/** 
* This class is responsible for two core things: 
* 
* One registering the dependencies and two creating 
* new instances of classes that you pass in as strings. 
* 
* Your data structure needs to be a function that retruns and array 
* which is then used in the register_dependencies() function. 
* 
* You would register the dependencies before you actually use this class 
* as you would a singleton. 
* 
* The data structure is as such: 
* 
* <code> 
* $array = array(
*  // This is a class that takes no parameters. 
*  'class_name' => array(), 
* 
*  // This is a class which takes in parameters. 
*  'class_name' => array(
*   'params' => array(
*    'params1', 
*    'params2' 
*    'optionalParams3' 
*  ) 
* ), 
*); 
* </code> 
* 
* You could then call the function, in this case foo() that returns this 
* array as such: 
* 
* <code> 
* AisisCore_Factory_Pattern::register_dependencies(foo()); 
* </code> 
* 
* Then else where you could do: 
* 
* <code> 
* $object = AisisCore_Factory_Pattern::create('class_name'); 
* $object->class_names_method(); 
* </code> 
* 
* And there ya go, you have created a class with its dependencies. 
* 
* 
* @author Adam Balan 
* 
*/ 
class AisisCore_Factory_Pattern { 

    /** 
    * @var AisisCore_Factory_Pattern 
    */ 
    protected static $_class_instance; 

    /** 
    * @var array 
    */ 
    protected static $_dependencies; 

    /** 
    * Create a new instance of the class assuming one 
    * does not already exist. 
    */ 
    public static function get_instance(){ 
     if(self::$_class_instance == null){ 
      $_class_instance = new self(); 
     } 

     return self::$_class_instance; 
    } 

    /** 
    * Create a new instance of a class with its dependencies. 
    * 
    * We expect there to be a class, we expect there to be dependcies 
    * already in the protected class level variable _dependencies and we 
    * expect that class to be in the array of depdencies. 
    * 
    * If it is not we throw an error. 
    * 
    * If the class has arguments we populate the classes params with the arguments. 
    * and if not we create a new instance of it. 
    * 
    * @param string $class 
    * @throws AisisCore_Exceptions_Exception 
    */ 
    public static function create($class){ 
     if(empty($class)){ 
      throw new AisisCore_Exceptions_Exception('Class cannot be empty.'); 
     } 

     if(!isset(self::$_dependencies)){ 
      throw new AisisCore_Exceptions_Exception('There is no dependencies arraty created. 
       Please create one and register it.'); 
     } 

     if(!isset(self::$_dependencies[$class])){ 
      throw new AisisCore_Exceptions_Exception('This class does not exist in the dependecies array!'); 
     } 

     if(isset(self::$_dependencies[$class]['params'])){ 
      $new_class = new $class(implode(', ', self::$_dependencies[$class]['params'])); 
      return $new_class; 
     }else{ 
      $new_class = new $class(); 
      return $new_class; 
     } 
    } 

    /** 
    * Register a new dependencie which is of type array. 
    * 
    * @param array $array 
    */ 
    public static function register_dependencies($array){ 
     self::$_dependencies = $array; 
    } 

} 

가장 중요한 부분은 만드는 방법에 if 문이다 콤마.