C #, a common way to access various lists in a class

I have a class of 3 different linked lists (for saving objects in the game I'm working on). Lists are all objects with the same base type, but I save them separately for processing. Note that IEntity, IObject, and IUndead are all inherited from IEntity.

public class EntityBucket
{
    public LinkedList<IEntity> undeadEntities;
    public LinkedList<IEntity> objects;
    public LinkedList<IEntity> livingEntities;

    public EntityBucket()
    {
        undeadEntities = new LinkedList<IEntity>();
        objects = new LinkedList<IEntity>();
        livingEntities = new LinkedList<IEntity>();
    }

    public LinkedList<IEntity> GetList(IObject e)
    {
        return objects;
    }

    public LinkedList<IEntity> GetList(IUndead e)
    {
        return undeadEntities;
    }

    public LinkedList<IEntity> GetList(ILiving e)
    {
        return livingEntities;
    }

}

3 , . , 3, , , - . - , -, . , GetList, , , . , :

public void Delete(IUndead e, World world)
{

     .....
     LinkedList<IEntity> list = buckets[k].GetList(e);
     .....
}

, (, ). , , , , . 3 ( 3 , add ..) - , IUndead, IObject ILiving. , .

, , , , .

, . , -, , , . getlist , .

+5
4

?

public class EntityBucket
{
  public LinkedList<IEntity> Entities;

  public IEnumerable<T> GetEntities<T>() where T : IEntity
  {
    return Entities.OfType<T>();
  }

}


List<IUndead> myBrainFinders = bucket.GetEntities<IUndead>().ToList();

(). , .

public class EntityBucket
{
  Dictionary<Type, List<IEntity>> entities = new Dictionary<Type, List<IEntity>>();

  public void Add<T>(T item) where T : IEntity
  {
    Type tType = typeof(T);
    if (!entities.ContainsKey(tType))
    {
      entities.Add(tType, new List<IEntity>());
    }
    entities[tType].Add(item);
  }

  public List<T> GetList<T>() where T : IEntity
  {
    Type tType = typeof(T);
    if (!entities.ContainsKey(tType))
    {
      return new List<T>();
    }
    return entities[tType].Cast<T>().ToList();
  }

  public List<IEntity> GetAll()
  {
    return entities.SelectMany(kvp => kvp.Value)
      .Distinct() //to remove items added multiple times, or to multiple lists
      .ToList();
  }

}
+1

, , , , GetList, , .

- :

public List<IEntity> GetList<T>() : where T:IEntity
{
    if(typeof(T)==typeof(IUndead)) return undedEntities;
    // and so on
}

: GetList<IUndead>();

, - :

enum EntityTypes { Undead, Alive, Object };
public List<IEntity> GetList(EntityTypes entityType) { ... }

.

EDIT: . - GetList a Zombie, IUndead, . - LiveZombie, IUndead IAlive. .

+3

:

public LinkedList<IEntity> GetList(Type type) {
    if (typeof(IUndead).IsAssignableFrom(type)) return undeadEntities;
    if (typeof(ILiving).IsAssignableFrom(type)) return livingEntities;
    if (typeof(IObject).IsAssignableFrom(type)) return objects;
}

:

var myUndeads = GetList(typeof(IUndead));
var myLivings = GetList(typeof(ILiving));
// etc

, , .

IsAssignableFrom (.. CatZombie, Zombie, IUndead, ). , Delete, :

public void Delete(IEntity e, World world) {
    if (typeof(IUndead).IsAssignableFrom(type)) undeadEntities.Remove(e);
    if (typeof(ILiving).IsAssignableFrom(type)) livingEntities.Remove(e);
    if (typeof(IObject).IsAssignableFrom(type)) objects.Remove(e);
}

EDIT: I noticed your comment regarding zmbq's answer regarding performance; it is definitely NOT fast. If you need high performance, use the enum style approach. Your code will be more detailed and require additional maintenance, but you will get much better performance.

+1
source

It seems to me that you could simply implement a dictionary called LinkedList and refer to them by name or listing.

Thus, adding or removing lists is simply an implementation problem and without a separate class to work with.

0
source

All Articles