Singleton与PHP5中的Factory模式结合 [英] Singleton in Conjunction with the Factory Pattern in PHP5

查看:76
本文介绍了Singleton与PHP5中的Factory模式结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP5中结合使用单例设计模式和工厂方法模式的最佳方法是什么?我最简单的用法是为每种数据库类型实例化选择性数据库连接一次。

What is the best method for using singleton design pattern in conjunction with the factory method pattern in PHP5? My simplest usage scenario for this is instantiation selective database connection only once for each database type.

推荐答案

用于数据库连接的单个工厂:

singleton factory for DB connection:

class Registry
{
    private static $_objects;

    public static function set($key, $object)
    {
        if (!array_key_exists($key, self::$_objects)) self::$_objects[$key] = $object;
    }

    public static function get($key)
    {
        if (array_key_exists($key, self::$_objects)) return self::$_objects[$key];
        else return false;
    }
}

class DBFactory
{
    public static function getConnection($type)
    {
        switch ($type) {
            case 'pdo':
                if (!(Registry::get('DB_PDO') instaceof DbPdo)) Registry::set('DB_PDO', new DbPdo('user', 'pass', ...));
                return Registry::get('DB_PDO')
            case 'mssql':
                //same for other connections
            //...
        }
    }
}

用法:

$factory = DBFactory::getConnection('pdo');

不再需要单子,因为所有方法都可以静态调用...
但是数据库类仍然可以视为单例,因为在您的应用程序中将只有一个实例。

Singletons are not really needed anymore because all methods can be called statically... But the database classes can still be considered singletons because there will only be one single instance of them in your application.

因此,通过使用工厂和注册表模式也可以产生相同的效果。

So the same effect is created by using the factory and registry patterns.

可以将注册表替换为使您的数据库类成为单例,则工厂将如下所示:

The registry could be replaced by making your database classes singletons then the factory would look like this:

class DBFactory
{
    public static function getConnection($type)
    {
        switch ($type) {
            case 'pdo':
                return DbPdo::getInstance('user', 'pass', ...);
            case 'mssql':
                //same for other connections
            //...
        }
    }
}

class DbPdo
{
    private static $_instance;

    private function __construct($user, $pass, ...){ //instantiate object }

    public static function getInstance($user = null, $pass = null, ...)
    {
        if (!(self::$_instance instanceof DbPdo)) self::$_instance = new DbPdo($user, $pass, ...);
        return self::$_instance;
    }
}

因此,您可以选择制作所有数据库对象单身人士或使用注册表。我个人会使用注册表,因为它可以用于存储任何类型的对象,甚至是您不想使类成为单例的对象。

So you have the choice of making all your DB objects singletons or using a registry. I personally would go with a registry because it can be used to store any types of object, even the ones where you don't want to make the class a singleton.

设计选择始终受个人风格的限制...

Design choices are always subjected to personal flavor imo...

这篇关于Singleton与PHP5中的Factory模式结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆