我有两个单独的实体列表:
class EntityCollection : IList<Entity>
{
//...
}
EntityCollection Foo;
EntityCollection Bar;
我想实现一个将Qux
列表 Foo 中的对象移动到 Bar 的操作。实施它的最佳方法是什么?
作为
MoveTo
实例方法EntityCollection
:public void MoveTo(EntityCollection to, Entity entity); // Client code Foo.MoveTo(Bar, Qux);
作为
MoveFrom
实例方法EntityCollection
:public void MoveFrom(EntityCollection from, Entity entity); // Client code Bar.MoveFrom(Foo, Qux);
作为静态
Move
方法EntityCollection
:public static void Move(Entity entity, EntityCollection from, EntityCollection to); // Client code EntityCollection.Move(Qux, Foo, Bar);
作为
Move
包含两个集合的类的实例方法:public void Move(Entity entity, EntityCollection from, EntityCollection to); // Client code Holder.Move(Qux, Foo, Bar);
或者,由于实体一次只能在一个集合中,我可以让实体自己跟踪它们的位置,并在实体本身上实现它:
public void MoveTo(EntityCollection to)
{
if(Location != null)
Location.Remove(this);
to.Add(this);
Location = to;
}
// Client code
Entity e;
e.MoveTo(Foo);
// Later on...
e.MoveTo(Bar);
当出现这么多选项时,我想知道:move方法属于哪里?为什么?