Suche…


Bemerkungen

Multitonitis

Genau wie Singleton kann Multiton als schlechte Praxis betrachtet werden. Es gibt jedoch Situationen, in denen Sie sie sinnvoll einsetzen können (beispielsweise, wenn Sie ein System wie ORM / ODM erstellen, um mehrere Objekte zu speichern).

Pool von Singletons (PHP-Beispiel)

Multiton kann als Container für Singletons verwendet werden. Bei dieser Multiton-Implementierung handelt es sich um eine Kombination aus Singleton- und Pool-Mustern.

Dies ist ein Beispiel dafür, wie die allgemeine Pool-Klasse "Multiton abstract" erstellt werden kann:

abstract class MultitonPoolAbstract
{
    /**
     * @var array
     */
    protected static $instances = [];

    final protected function __construct() {}

    /**
     * Get class name of lately binded class
     *
     * @return string
     */
    final protected static function getClassName()
    {
        return get_called_class();
    }

    /**
     * Instantiates a calling class object
     *
     * @return static
     */
    public static function getInstance()
    {
        $className = static::getClassName();

        if( !isset(self::$instances[$className]) ) {
            self::$instances[$className] = new $className;
        }

        return self::$instances[$className];
    }

    /**
     * Deletes a calling class object
     *
     * @return void
     */
    public static function deleteInstance()
    {
        $className = static::getClassName();

        if( isset(self::$instances[$className]) )
            unset(self::$instances[$className]);
    }

    /*-------------------------------------------------------------------------
    | Seal methods that can instantiate the class
    |------------------------------------------------------------------------*/

    final protected function __clone() {}

    final protected function __sleep() {}

    final protected function __wakeup() {}
}

Auf diese Weise können wir verschiedene Singleton-Pools instanziieren.

Registrierung von Singletons (PHP-Beispiel)

Dieses Muster kann verwendet werden, um registrierte Pools von Singletons zu enthalten, die sich jeweils durch eine eindeutige ID unterscheiden:

abstract class MultitonRegistryAbstract
{
    /**
     * @var array
     */
    protected static $instances = [];

    /** 
     * @param string $id
     */
    final protected function __construct($id) {}

    /**
     * Get class name of lately binded class
     *
     * @return string
     */
    final protected static function getClassName()
    {
        return get_called_class();
    }

    /**
     * Instantiates a calling class object
     *
     * @return static
     */
    public static function getInstance($id)
    {
        $className = static::getClassName();

        if( !isset(self::$instances[$className]) ) {
            self::$instances[$className] = [$id => new $className($id)];
        } else {
            if( !isset(self::$instances[$className][$id]) ) {
                self::$instances[$className][$id] = new $className($id);
            }
        }

        return self::$instances[$className][$id];
    }

    /**
     * Deletes a calling class object
     *
     * @return void
     */
    public static function unsetInstance($id)
    {
        $className = static::getClassName();

        if( isset(self::$instances[$className]) ) {
            if( isset(self::$instances[$className][$id]) ) {
                unset(self::$instances[$className][$id]);
            }

            if( empty(self::$instances[$className]) ) {
                unset(self::$instances[$className]);
            }
        }
    }

    /*-------------------------------------------------------------------------
    | Seal methods that can instantiate the class
    |------------------------------------------------------------------------*/

    final protected function __clone() {}

    final protected function __sleep() {}

    final protected function __wakeup() {}
}

Dies ist eine vereinfachte Form eines Musters, das für ORM verwendet werden kann, um mehrere Entitäten eines bestimmten Typs zu speichern.



Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow