为过时对象的集合选择数据结构
我正在尝试设计一个 PHP 对象(将其称为 Incident_Collection
),它将保存其他对象的集合,每个对象都实现 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
并将 Incident 对象存储在集合的数组属性中:
<?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 数据结构 可能更合适/更有效。 但是哪一个?我不太清楚何时使用特定的数据结构。
另一个问题是,Incident_Collection
可能存在限制。例如,如果有一个 Person
对象拥有一个 Incident_Collection
,则可能会应用以下限制:
- 只有 1 个
Birth
事件, - 如果
Birth
存在,它必须是集合中最早的事件 - 只有 1 个
Death
事件 - 如果
Death
存在,它必须是集合中的最后一个事件 HS_Graduation
必须在HS_Begin
之后
最好有一个通用的 Incident_Collection
来接受来自其所有者的一组限制(例如 Person
>),还是子类 Person_Incident_Collection
?
I'm trying to design a PHP object (call it Incident_Collection
) that will hold a collection of other objects each of which implement an Incident
interface.
<?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;
}
}
?>
At first I figured I'd just make my Incident_Collection
implement IteratorAggregate
and store the Incident objects in an array property of the collection:
<?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*/
}
?>
But because Incident
objects have a natural order, I thought perhaps extending one of the SPL Data Structures might be more appropriate/efficient. But which one? I'm not very clear about when to use a particular data structure.
Another wrinkle is that there may be restrictions on an Incident_Collection
. For example, if there were a Person
object who had an Incident_Collection
, perhaps the following restrictions might apply:
- only 1
Birth
incident - if
Birth
exists, it must be the earliest incident in the collection - only 1
Death
incident - if
Death
exists, it must be the last incident in the collection HS_Graduation
must come afterHS_Begin
Would it be better to have a generic Incident_Collection
that accepts a set of restrictions from its owner (e.g. Person
), or a subclassed Person_Incident_Collection
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
它很好地概述了 SPL 数据结构、它们是什么以及何时使用它们。还有基准。
如果这是一个对象集合,我肯定会考虑使用 SplObjectStorage 而不是普通数组。如果事件应按 LIFO 或 FIFO 顺序,请考虑队列和堆栈。如果您需要按自定义顺序排列它们,请考虑优先级队列。
关于限制,您可以使用 状态模式,例如通过一般 IncidentCollection 进行访问,但取决于它的所有者属性,应用子类来处理状态的更改。但这要求该集合具有所有者属性。因为无论如何,各个状态都是 IncidentCollection 的子类,所以您也可以直接使用它们。
Check out
It gives a good overview of the SPL DataStructures, what they are and when you want to use them. There is also benchmarks.
If this is an object collection, I'd definitely consider using SplObjectStorage instead of the plain Array. If the Incidents should be in LIFO or FIFO order consider queues and stacks. If you need them in a custom order, consider a Priority Queue.
Regarding the restrictions, you could use a State Pattern, e.g. access happens through the general IncidentCollection, but depending on it's owner property, a subclass is applied to handle changing of state. This requires the collection to have a owner property though. Because the individiual states are subclasses of IncidentCollection anyway, you could just as well use them directly though.