Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about something like the following?</p> <pre><code>public LinkedList&lt;IEntity&gt; GetList(Type type) { if (typeof(IUndead).IsAssignableFrom(type)) return undeadEntities; if (typeof(ILiving).IsAssignableFrom(type)) return livingEntities; if (typeof(IObject).IsAssignableFrom(type)) return objects; } </code></pre> <p>Then you would call it like this:</p> <pre><code>var myUndeads = GetList(typeof(IUndead)); var myLivings = GetList(typeof(ILiving)); // etc </code></pre> <p>The same type of logic could be implemented in your deletes, add, and other methods, and you never need a concrete instance of an object to access them. </p> <p>The <code>IsAssignableFrom</code> logic handles subclassing just fine (i.e. you could have a <code>CatZombie</code>, which derives from <code>Zombie</code>, which implements <code>IUndead</code>, and this would still work). This means you still only have to create one Delete method, something like the following:</p> <pre><code>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); } </code></pre> <p><strong>EDIT:</strong> I noticed your comment on zmbq's answer regarding performance; this is definitely NOT fast. If you need high performance, use an enum-style approach. Your code will be more verbose and require more maintenance, but you'll get much better performance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload