具有每值单例的模式的名称是什么?

时间:2014-08-10 06:17:14

标签: design-patterns

我想知道我使用的是什么类型的模式。我在工厂,代理和multiton之间感到困惑。感谢您的回复。

class Base extends Database
    {
        /**
         *  base class that contains common methods through 
         *  out the application, and loading of application configs
         */
    }

    class MyObjectsBase extends Base
    {
        /**
         * base class for my objects
         */
    }

    interface MyInterface
    {
        /**
         * interface methods
         */
    }

    class MyObj1 extends MyObjectsBase implements MyInterface
    {
        /**
         * MyObj1 class
         */
    }

    class MyObj2 extends MyObjectsBase implements  MyInterface
    {
        /**
         * MyObj2 class
         */
    }

    class PatternInQuestion
    {
        private static $singleInstanceOfType = array();


        public static function getInstance ($objType)
        {
            if ( ! array_key_exists($objType, self::$singleInstanceOfType) ) {
                self::$singleInstanceOfType[$objType] = new $objType();
            }

            return self::$singleInstanceOfType[$objType];

        }

    }

    /**
     * Usage :
     */

    $type = 'MyObj2';

    $obj = PatternInQuestion::getInstance($type);

我想知道我使用的是什么类型的模式。我在工厂,代理和multiton之间感到困惑。谢谢你的回复。

1 个答案:

答案 0 :(得分:0)

这是multiton模式的实现。

  

在软件工程中,多重模式是一种类似于单例的设计模式,它只允许创建一个类的一个实例。 multiton模式扩展了单例概念,将命名实例的映射作为键值对进行管理。