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か?