php手冊中的工廠方法
•使用工廠來替換new操作
•思路:動态的根據傳遞的資料,建立相應的類的對象。
•<?php
class example
{
// the parameterized factory method
public static function factory(type) { if (include_once 'drivers/' .type) { if (include_once 'drivers/' .type . '.php') {
classname = 'driver_' .classname = 'driver_' .type;
return new $classname;
} else {
throw new exception('driver not found');
}
}
}
?>
// load a mysql driver
mysql=example::factory(′mysql′);//loadansqlitedrivermysql=example::factory(′mysql′);//loadansqlitedriversqlite = example::factory('sqlite');
gof的工廠方法模式
•簡單工廠模式---工廠方法模式
•工廠方法模式是建立型模式的一種
•意圖:定義一個用于建立對象的接口,讓子類決定執行個體化哪一個類。工廠方法模式能使一個類的執行個體化延遲到其子類。

工廠方法實作
适用的需求
•1 生成”履歷”或者”報告”兩種文檔格式
•2 履歷文檔頁面:
–技能頁 + 教育經曆頁 + 工作經曆頁
•3 報告文檔頁面:
–介紹頁面 + 闡述結論頁面 + 總結頁
•4 以後很有可能增加新的文檔格式,新的文檔格式可能會有新的文檔頁面
•1 建立document類
•2 建立繼承于document的report類和resume類
•3 建立page類
•4 建立繼承于page的skillspage,educationpage,experiencepage類
•5 建立繼承于page的introductionpage,resultpage,summarypage類
•document類的實作
•resume類實作
•program