Yii2設計模式——靜態工廠模式

應用舉例

yii\db\ActiveRecord編程

//獲取 Connection 實例
public static function getDb()
{
	return Yii::$app->getDb();
}

//獲取 ActiveQuery 實例 
public static function find()
{
	return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
}

這裏用到了靜態工廠模式。設計模式

靜態工廠

利用靜態方法定義一個簡單工廠,這是很常見的技巧,常被稱爲靜態工廠(Static Factory)。靜態工廠是 new 關鍵詞實例化的另外一種替代,也更像是一種編程習慣而非一種設計模式。和簡單工廠相比,靜態工廠經過一個靜態方法去實例化對象。爲什麼使用靜態方法?由於不須要建立工廠實例就能夠直接獲取對象。app

和Java不一樣,PHP的靜態方法能夠被子類繼承。當子類靜態方法不存在,直接調用父類的靜態方法。無論是靜態方法仍是靜態成員變量,都是針對的類而不是對象。所以,靜態方法是共用的,靜態成員變量是共享的。yii

代碼實現

//靜態工廠
class StaticFactory
{
    //靜態方法
    public static function factory(string $type): FormatterInterface
    {
        if ($type == 'number') {
            return new FormatNumber();
        }

        if ($type == 'string') {
            return new FormatString();
        }

        throw new \InvalidArgumentException('Unknown format given');
    }
}

//FormatString類
class FormatString implements FormatterInterface
{
}

//FormatNumber類
class FormatNumber implements FormatterInterface
{
}

interface FormatterInterface
{
}

使用:this

//獲取FormatNumber對象
StaticFactory::factory('number');

//獲取FormatString對象
StaticFactory::factory('string');

//獲取不存在的對象
StaticFactory::factory('object');

Yii2中的靜態工廠

Yii2 使用靜態工廠的地方很是很是多,比簡單工廠還要多。關於靜態工廠的使用,咱們能夠再舉一例。spa

咱們可經過重載靜態方法 ActiveRecord::find() 實現對where查詢條件的封裝:.net

//默認篩選已經審覈經過的記錄
public function checked($status = 1)
{
	return $this->where(['check_status' => $status]);
}

和where的鏈式操做:設計

Student::find()->checked()->where(...)->all();
Student::checked(2)->where(...)->all();

詳情請參考個人另外一篇文章 Yii2 Scope 功能的改進code

相關文章
相關標籤/搜索