5

私は次のようなシングルトン クラスのインスタンスを作成していました。

$Singleton = SingletonClassName::GetInstance();

非シングルトンクラスの場合:

$NonSingleton = new NonSingletonClassName;

これがシングルトンであるかどうかにかかわらず、クラスのインスタンスを作成する方法を区別すべきではないと思います。他のクラスの認識を見ると、クラスがシングルトンクラスを必要とするかどうかは気にしません。そのため、php がシングルトン クラスをどのように扱うかについては、まだ満足していません。私は考え、常に書きたいと思っています:

$Singleton = new SingletonClassName;

別の非シングルトン クラスですが、この問題の解決策はありますか?

4

5 に答える 5

3

逆のほうがいいです -非シングルトンのファクトリメソッドを提供し、以下を使用してそれらのインスタンスを取得します。

$NonSingleton = NonSingletonClassName::createInstance();

これは Java で推奨されるベスト プラクティスですが ( Effective Javaで)、ほとんどのオブジェクト指向言語に適用されます。

于 2010-04-06T12:59:34.870 に答える
2

コードを理解するのが非常に難しくなるので、お勧めしません(人々は、新しいとはまったく新しいオブジェクトを意味すると考えています)。しかし、私はシングルトンの使用を推奨しませんでした。

このコードの基本的な考え方は、シングルトンの周りにラッパーがあるということです。そのラッパーを介してアクセスされるすべての関数と変数は、実際にはシングルトンに影響します。以下のコードは多くのマジックメソッドとSPLインターフェイスを実装していないため、完全ではありませんが、必要に応じて追加できます。

コード

/**
 * Superclass for a wrapper around a singleton implementation
 *
 * This class provides all the required functionality and avoids having to copy and
 * paste code for multiple singletons.
 */
class SingletonWrapper{
    private $_instance;
    /**
     * Ensures only derived classes can be constructed
     *
     * @param string $c The name of the singleton implementation class
     */
    protected function __construct($c){
        $this->_instance = &call_user_func(array($c, 'getInstance'));
    }
    public function __call($name, $args){
        call_user_func_array(array($this->_instance, $name), $args);
    }
    public function __get($name){
        return $this->_instance->{$name};
    }
    public function __set($name, $value){
        $this->_instance->{$name} = $value;
    }
}

/**
 * A test singleton implementation. This shouldn't be constructed and getInstance shouldn't
 * be used except by the MySingleton wrapper class.
 */
class MySingletonImpl{
    private static $instance = null;
    public function &getInstance(){
        if ( self::$instance === null ){
            self::$instance = new self();
        }
        return self::$instance;
    }

    //test functions
    public $foo = 1;
    public function bar(){
        static $var = 1;
        echo $var++;
    }
}

/**
 * A wrapper around the MySingletonImpl class
 */
class MySingleton extends SingletonWrapper{
    public function __construct(){
        parent::__construct('MySingletonImpl');
    }
}

$s1 = new MySingleton();
echo $s1->foo; //1
$s1->foo = 2;

$s2 = new MySingleton();
echo $s2->foo; //2

$s1->bar(); //1
$s2->bar(); //2
于 2010-04-06T13:39:36.580 に答える
1

通常のクラス インスタンスと同じ方法でシングルトンを作成することはできません。newは常に新しいインスタンスを返すため、コンストラクターを非パブリックにする必要があり、クラス内から呼び出すには別の手段が必要です。

すべてのクラスにファクトリメソッドを設定できます。たとえば、常にgetInstance()別の回答に示されているようにします。もう 1 つのオプションは、何を返すかどうかを認識しているService LocatorDependency Injection Frameworkを使用することです。

于 2010-04-06T13:04:34.423 に答える
1

キーワードが意味するものに基づいて、必要なものnewはすべて無関係です。クラスの新しいインスタンスを作成します。それがnewnewと名付けられた理由です:-)

于 2010-04-06T13:06:28.510 に答える
1

これは非常に古い投稿であることは知っていますが、以下のリンクの助けを借りて Factory パターンをよりよく理解できました。

それで、それが未来をさまよったのを助けるなら。. . . ここに行きます

http://www.devshed.com/c/a/PHP/Design-Patterns-in-PHP-Factory-Method-and-Abstract-Factory/1/

基本的な実装を把握して理解するのは非常に簡単でした。

于 2012-06-17T18:11:29.500 に答える