2011-02-17 51 views
2

假設我有這個經典的開關,我知道當我們建立類不是一個很好的做法使用開關方法,所以,我怎麼可以重建這個成類沒有使用開關多態性,我想了解的方法。PHP5 - 面向對象 - 多態 - 幫我改寫這個簡單的開關

/** 
* globals below are holding unique id 
* $Franklin['Franklin_id'] , 
* $Granny_Smith['Granny_Smith_id'] , 
* etc etc... 
*/ 

global $Fuji, $Gala, $Franklin, $Granny_Smith; 

switch($Apple) { 
    case 'Fuji': 
    $Color = 'Yellowish green'; 
    $Size = 'medium'; 
    $Origin = 'Japan'; 
    $Season = 'October - January'; 
    $AppleId = $Fuji['Fuji_id']; 
    break; 
    case 'Gala': 
    $Color = 'yellow'; 
    $Size = 'medium'; 
    $Origin = 'New Zealand'; 
    $Season = 'October - January'; 
    $AppleId = $Gala['Gala_id']; 
    break; 
    case 'Franklin': 
    $Color = 'Well-colored'; 
    $Size = 'medium'; 
    $Origin = 'Ohio'; 
    $Season = 'October'; 
    $AppleId = $Franklin['Franklin_id']; 
    break; 
    case 'Granny_Smith': 
    $Color = 'Green'; 
    $Size = 'medium'; 
    $Origin = 'Australia'; 
    $Season = 'October - December'; 
    $AppleId = $Granny_Smith['Granny_Smith_id']; 
    break; 
} 

然後我想能夠使用它像這樣

$AppleProps = new getApple('Granny_Smith'); // $AppleProps->Color, etc etc 

預先感謝您,希望這可以幫助別人。

親切的問候

盧卡

+1

*(參考)* [更換條件與多態性(http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism) – Gordon 2011-02-17 18:41:12

+0

@Gordon - 謝的好友,這絕對要求第二部分或題。 ;) – 2011-02-17 18:44:02

回答

3

我沒有完成確認的是您的ID的意思,但是這個代碼給你一個AppleFactory是將「郵票」每一個新的蘋果一個唯一的ID。

class AppleFactory { 

    static $id = 0; 

    static public function getApple($className) { 
     $apple = new $className(); 
     $apple->id = self::$id++; 
     return $apple; 
    } 

} 

class Apple { 

    public $id; 
    public $color; 
    public $size; 
    public $origin; 
    public $season; 

} 

class GrannySmith extends Apple { 

    public function __construct() { 
     $this->color = 'Green'; 
     $this->size = 'medium'; 
     $this->origin = 'Australia'; 
     $this->season = 'October - Desember'; 
    } 

} 

$a = AppleFactory::getApple('GrannySmith'); 
print_r($a); 
3

沒有必要對面向對象在這裏。但switch可以被更簡單的構造所取代。如果您使用的數據陣列,你甚至可以跳過功能:

$apple_data = array(
    'Fuji' => array(
    'Color' => 'Yellowish green'; 
    'Size' => 'medium'; 
    'Origin' => 'Japan'; 
    'Season' => 'October - January'; 
    'AppleId' = 1234567890, 
), 
    'Gala' => array(
    'Color' => 'yellow'; 
    'Size' => 'medium'; 
    'Origin' => 'New Zealand'; 
    'Season' => 'October - January'; 
    'AppleId' => 1234598760, 
), 
    ... 
); 

要訪問的屬性只需要使用:

$id = $apple_data["Granny_Smith"]["AppleId"] 

或者,如果你真的想所有這些局部變量:

extract($apple_data["Granny_Smith"]); 
// creates $Color, $Size, $Origin, $Season, $AppleId in local scope 

如果您確實需要對象語法,請嘗試:

$AppleProps = new ArrayObject($apple_data["Fuji"], 2); 
print $AppleProps->Color; 

但是由於蘋果沒有做任何事情,你可能不想爲它們創建一個類或真實對象。 (該死的蘋果,只是坐在那裏,什麼都不做。)

+0

+1的ArrayObject():P但我已經在使用數組。 – 2011-02-17 18:11:04

5

如果你真的想用OO這一點,那麼你應該是創建一個appleFactory類,然後對每個種類的蘋果都單獨的類.. 。

class appleFactory 
{ 
    public static function getApple($name) 
    { 
     $className = $name.'_apple'; 

     return new $className(); 
    } 
} 

class fuji_apple 
{ 
    public function __construct() 
    { 
     $this->color = 'Yellowish green'; 
     $this->size = 'medium'; 
     $this->origin = 'Japan'; 
     $this->season = 'October - January'; 
     $this->appleId = $Fuji['Fuji_id']; 
    } 
} 

class gala_apple 
{ 
    public function __construct() 
    { 
     $this->color = 'Yellow'; 
     $this->size = 'medium'; 
     $this->origin = 'New Zealand'; 
     $this->season = 'October - January'; 
     $this->appleId = $Gala['Gala_id']; 
    } 
} 

然後使用它是這樣的...

$fuji = appleFactory::get('fuji'); 
$gala = appleFactory::get('gala'); 
+0

這是很好的+1;) – 2011-02-17 18:09:28