1

オブジェクトのセットが必要で、それぞれが独自の一意の ID を持つ必要があるとします。オブジェクトのタイプを示す文字と、作成されたオブジェクトの数を示す数字だけです。たとえば、a0、a1、b0、c0、c1、c2、c3 などです。

グローバル変数を設定して、各オブジェクトが既にいくつ存在するかを追跡するのではなく、クラスを使用して追跡したいと考えています。そのようです:

class uniqueIDGenerator
{
  private $numberAObjs;
  private $numberBObjs;
  private $numberCObjs;

  public function generateID ($type) {
    if($type === "A") {
      return 'a' . (int) $this->$numberAObjs++;
    } else if($type === "B") {
      return 'b' . (int) $this->$numberBObjs++;
    } else if($type === "C") {
        return 'c' . (int) $this->$numberCObjs++;
    }
  }
}

class obj
{
  private $id;

  function __construct($type) {
    $this->id = uniqueIDGenerator::generateID($type);
  }
}

これの問題は、uniqueIDGenerator がインスタンス化されていない場合、プライベート変数が実際にはメモリ内に作成されていないため、generateID 関数が常に各型 (a0、b0、c0 など) に対して同じ値を返すことです。同時に、それを obj のプロパティにすることは機能しません。なぜなら、obj が作成されるたびに、それは uniqueIDGenerator の独自のインスタンスを持つことになるため、常に a0、b0、c0 も返されるからです (それが呼び出されるだけであると仮定します)。そのオブジェクトのメソッドで 1 回) など。

唯一のオプションは、uniqueIDGenerator を独自のグローバル インスタンスにして、obj のコンストラクターがそれを参照できるようにすることですが、それはコーディング プラクティスが不十分なようです。すべてのオブジェクトを分離して整理する、これを行うための適切な OOP 方法はありますか?

4

1 に答える 1

1

First you can modify object constructor:

class obj
{
  private $id;

  function __construct($type, $id) {
    $this->id = $id;
  }
}

...

$id_generator = new uniqueIDGenerator(); // instanciation of the generator

$obj1 = new obj(type, $id_generator->generateID($type));
$obj2 = new obj(type, $id_generator->generateID($type));
$obj3 = new obj(type, $id_generator->generateID($type));
...

In my projects, I would create a class named ObjectFactory:

    class ObjectFactory {
       private $id_generator;

       public function __construct($id_generator) {
          $this->id_generator = $id_generator;
       }

       public function create_object($type) {
          return new obj($this->id_generator->generateID($type));
       }
    }

...

$id_generator = new uniqueIDGenerator(); // instanciation of the generator
$obj_factory = new ObjectFactory($id_generator); 

$obj1 = obj_factory->create_object($type);
$obj2 = obj_factory->create_object($type);
$obj3 = obj_factory->create_object($type);

Finaly, to avoid the use of a global instance of this class, you can do a Singleton (adapted to your situation):

class uniqueIDGenerator
{
  private $numberAObjs;
  private $numberBObjs;
  private $numberCObjs;

  public static $instance = null;

  public function __construct() {
    $numberAObjs = 0;
    $numberBObjs = 0;
    $numberCObjs = 0;
  }

  public static function generateID($type) {
     if(!self::$instance)
        self::$instance = new uniqueIDGenerator();

     return self::$instance->generateID2($type);
  }

  private function generateID2 ($type) {
    if($type === "A") {
      return 'a' . (int) $this->numberAObjs++;
    } else if($type === "B") {
      return 'b' . (int) $this->numberBObjs++;
    } else if($type === "C") {
        return 'c' . (int) $this->numberCObjs++;
    }
  }
}

uniqueIDGenerator::generateID("A");
于 2013-10-30T17:19:24.837 に答える