2013-01-18 33 views
0

我正在尋找一些幫助,我寫了一個工廠模式,幾乎做我需要的,接受它會返回一個基於參數的新類。在php中使用工廠模式來構建汽車對象

我需要的是一種工廠模式,它接受一些東西並返回一個類型爲汽車的對象。該車將包含類型,型號,年份,製造和里程錶讀數等信息。

這是我AVE至今:

<?php 
class Car_Factory_Pattern{ 

    protected $_instance; 

    protected $_dependencies; 

    public function getInstance(){ 
     if(null == self::$_instance){ 
      self::$_instance == new self(); 
     } 

     return self::$_instance; 
    } 

    public function create($class){ 
     if(empty($class)){ 
      throw new Exception('Cannot declare and empty class.'); 
     } 

     if(!isset(self::$_dependencies)){ 
      throw new Exception('The $dependencies are not set for this class'); 
     } 

     if(!isset(self::$_dependencies[$class])){ 
      throw new Exception('This class does not exist in the dependencies 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; 
     }  
    } 

    public function registerDependencies(array $array){ 
     self::$_dependencies = $array; 
    } 
} 

你就需要爲這個數據結構爲:

函數依賴(){

$dependencies = array(
    'Car_Class' => array(
     'params' => array(
      'Type', 
          'Model', 
          ... 
     ), 
    ),   
); 

return $dependencies; 

}

然後你通過這樣做來使課堂瞬間完成:

$factory = Car_Factory_Pattern()::getInstance(); 
$factory->registerDependencies(dependencies()); 

,然後讓我做的事:

$some_car = Car_Factory_Pattern()::create('Car_Class'); 

這樣做的問題是,我基本上是硬編碼的依賴關係,這適用於其他應用程序我工作,怎麼過 - 我需要這個什麼上課要做的是採取類型,模型,製作,年份和odom閱讀,並給我一個類型汽車的對象,無論你通過什麼,我可以創建一個類,它需要一系列的選項,我曾經希望如何工廠模式會爲我做到這一點 - 除非我錯了?

感謝您的幫助。

$new_class = new $class('Type,Model,Etc,Etc,will automaticly get more values depending on your array'); 

那是你真正想要的:

+0

一個理智的使用工廠模式是爲了訪問單例對象,但是你的工廠總是做一個新的對象!那是你要的嗎? –

回答

0

THT的我工廠的想法。 該類保留每個新類的單例,並且所有汽車類都擴展了一個基類car_class,該car_class使您可以設置依賴關係。 這種方法節約資源,創造一個新的汽車對象每次 感謝@NiclasLarsson爲魔術得到

<?php 
class Car_Factory_Pattern{ 

    protected $_instance; 

    public static function getInstance($class){ 
     if(null == self::$_instance){ 
      self::$_instance[$class] = new $class(); 
     } 
     return self::$_instance[$class]; 

    } 
} 

class Base_Car { 
private $aVariables = array(); 

public function __construct() { 
    // to oveload 
} 
public function populate($aData = array()) { 
    $this->aVariables = $aData; 
} 

public function __get($sKey) { 
    return isset($this->aVariables[$sKey]) ? $this->aVariables[$sKey] : NULL; 
} 
} 


class Example_Car extend Base_Car { 

public function __construct() { 
    // do heavy initialization 
} 

    // other functions 

} 

$car = Car_Factory_Pattern::getInstance('Example_class'); 
$car->populate(depencies()); 

ADD

使用@NiclasLarsson Example_Car類

​​
+0

與這個問題是,我想能夠創建多輛車,如果你看到的數據結構,我可以在技術上只有一輛車... – Adam

+0

所以,你是不需要一個工廠,但只有神奇的方法,或創建汽車對象並存儲屬性的簡單靜態類 –

+0

這就是我的想法。我接受你的答案是正確的。 – Adam

0
$new_class = new $class(implode(', ', self::$_dependencies[$class]['params'])); 

像下面該代碼段會產生些什麼呢? 如果您想將它們作爲參數傳遞,可以使用ReflectionClass類。

如果您願意通過所有「PARAMS」作爲一個數組,這將是mutch簡單:

在類的「Example_Car」你可以有這樣的:

class Example_Car { 
private $aVariables = array(); 

public function __construct($aData = array()) { 
    $this->aVariables = $aData; 
} 

public function __get($sKey) { 
    return isset($this->aVariables[$sKey]) ? $this->aVariables[$sKey] : NULL; 
} 
} 


$factory = Car_Factory_Pattern()::getInstance(); 
$factory->registerDependencies(dependencies()); 

$some_car = Car_Factory_Pattern()::create('Example_Car'); 

var_dump($some_car->Type, $some_car->Model); 
+0

如果您查看用於工廠模式的數據結構,則可以在params下傳遞所有信息的數組。 – Adam