Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Add another level of indirection. The <code>GetAvailableSpells</code> method isn't really an instance method, since it's the same for all instances. As you pointed you, you can't have an abstract static method, so instead move the type-specific stuff into an instance-based class factory. In the example below, <code>AvailableSpells</code> is a method of the <code>MagicSchool</code> abstract class, which has concrete subclasses <code>BlackMagic</code>, <code>WhiteMagic</code>, etc. The <code>Wizard</code> also has sub-types, but every <code>Wizard</code> can return the <code>MagicSchool</code> that it belongs to, giving you a type-safe, type-independent way to find out what the spells for any given <code>Wizard</code> object are without separate tables or code duplication.</p> <pre><code>public abstract class MagicSchool { public abstract string[] AvailableSpells { get; } public abstract Wizard CreateWizard(); } public abstract class Wizard { protected Wizard(MagicSchool school) { School = school; } public abstract Cast(string spell); MagicSchool School { public get; protected set; } } public class BlackMagic : MagicSchool { public override AvailableSpells { get { return new string[] { "zoogle", "xclondon" }; } } public override Wizard CreateWizard() { return new BlackWizard(this); } } public class BlackWizard : Wizard { public BlackWizard(BlackMagic school) : base(school) { // etc } public override Cast(string spell) { // etc. } } // continue for other wizard types </code></pre>
 

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