設計模式之-工廠模式

<?php
/* 
 * 工廠模式 工廠模式提供獲取某個對象的新實例的一個接口,同時使用代碼避免確定實際實例化的步驟
 */


class CD{

    public $title = "";
    public $band = "";
    public $tracks = array();

    public function  __construct() {

    }

    public function setTitle( $title){
        $this->title = $title;
    }

    public function setBand( $band ){
        $this->band = $band;
    }

    public function addTrack( $track ){
        $this->tracks[] = $track;
    }

}


class enhancedCD{

    public $title = "";
    public $band = "";
    public $track = array();

    public function setTitle( $title ){
        $this->title = $title;
    }

    public function setBand( $band ){
        $this->band = $band;
    }

    public function addTrack( $track ){
        $this->track[] = $track;

    }
}

class CDFactory{

    public static function create( $type ){
        $class = strtolower($type)."CD";
        return new $class;
    }
}

$type = "enhanced";
$cd = CDFactory::create($type);
//業務邏輯

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章