I have two separate lists of entities:
class EntityCollection : IList<Entity>
{
//...
}
EntityCollection Foo;
EntityCollection Bar;
I want to implement an operation to move an object Qux
that is on list Foo to Bar. What's the best way to implement it?
As a
MoveTo
instance method onEntityCollection
:public void MoveTo(EntityCollection to, Entity entity); // Client code Foo.MoveTo(Bar, Qux);
As a
MoveFrom
instance method onEntityCollection
:public void MoveFrom(EntityCollection from, Entity entity); // Client code Bar.MoveFrom(Foo, Qux);
As a static
Move
method onEntityCollection
:public static void Move(Entity entity, EntityCollection from, EntityCollection to); // Client code EntityCollection.Move(Qux, Foo, Bar);
As a
Move
instance method on the class that holds boths collections:public void Move(Entity entity, EntityCollection from, EntityCollection to); // Client code Holder.Move(Qux, Foo, Bar);
Alternatively, and since the entities can only be in one collection at a time, I could have the entities track their location themselves, and implement it on the entity itself:
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);
When presented with so many options, I want to know: where does the move method belong? And why?