Incident_Collection
それぞれがインターフェイスを実装する他のオブジェクトのコレクションを保持するPHP オブジェクト (と呼びます) を設計しようとしていIncident
ます。
<?php
class Foo implements Incident {
protected $incident_date; //DateTime object
protected $prop1;
protected $prop2;
//etc
public function when(){ //required by Incident interface
return $this->incident_date;
}
}
?>
最初は、インシデント オブジェクトをIncident_Collection
実装して、コレクションの配列プロパティに格納するだけでよいと考えました。IteratorAggregate
<?php
class Incident_Collection implements IteratorAggregate {
protected $collection=array();
public function getIterator(){
return new ArrayIterator($this->collection);
}
public function sort(){
//sort by $incident->when() values in $this->collection
}
/*also __get($var), __set($var,$value), add(Incident $object), remove(Incident $object) and other functions*/
}
?>
しかし、Incident
オブジェクトには自然な順序があるため、おそらくSPL データ構造の 1 つを拡張する方がより適切/効率的であると考えました。しかし、どれですか?特定のデータ構造をいつ使用するかについてはよくわかりません。
もう 1 つの問題は、Incident_Collection
. たとえば、 を持つPerson
オブジェクトがあった場合Incident_Collection
、次の制限が適用される可能性があります。
- 1
Birth
件のみ - 存在する場合
Birth
は、コレクション内の最初のインシデントである必要があります - 1
Death
件のみ - 存在する場合
Death
は、コレクション内の最後のインシデントである必要があります HS_Graduation
後に来なければならないHS_Begin
Incident_Collection
所有者からの一連の制限を受け入れるジェネリック(例: Person
)、またはサブクラス化した方がよいでしょうPerson_Incident_Collection
か?