title: Multiple classes with similar implementation, used with different corresponding classes?
body: <p>In a C# project, I'm stuck in a situation where I'm writing a lot of code to implement very similar functionality. I feel like I should be able to refactor this somehow, but I'm not sure what to do.</p>
  
  <p>In short, I have a list of classes <code>AClass</code>, <code>BClass</code>, etc. that inherit from a base class. Each class can accept a list of conditions unique to that class. For example, <code>AClass</code> can accept conditions of type <code>ACondition</code>, and <code>BClass</code> can accept conditions of type <code>BCondition</code>. Each condition class is an abstract class in the factory model that can accept nested conditions of a common set of derived classes, "AndCondition", "OrCondition", "NotCondition", so that the usage is something like the below example.</p>
  
  <pre><code>AClass foo = new AClass();
  foo.SetConditions(ACondition.And(ACondition.Factory1(),
      ACondition.Not(ACondition.Factory2())));
  </code></pre>
  
  <p>Of course, I could write the same kind of thing for <code>BClass</code>, with <code>BCondition</code> having different factories. My thought was to create a generic abstract class and inherit from it.</p>
  
  <pre><code>public abstract Condition&lt;T&gt;
  {
      public static Condition&lt;T&gt; And(Condition&lt;T&gt; c1, Condition&lt;T&gt; c2)
      {
          // Returns an AndCondition derived from Condition&lt;T&gt;.
      }
  
      // Other common implementation.
  }
  
  public abstract ACondition : Condition&lt;AClass&gt;
  {
      public static ACondition Factory1()
      {
          // Returns some class derived from ACondition.
      }
  
      // Other factory implementations.
  }
  </code></pre>
  
  <p>There are a couple of problems with this. First, at present, I'm not using <code>T</code> for anything except to distinguish between different instances of this class. This is necessary to prevent accidental (or intentional) use of a <code>BCondition</code> where an <code>ACondition</code> is expected. However, it doesn't prevent someone from creating another class derived from <code>Condition&lt;AClass&gt;</code> and using it where it's not expected. In other words, someone can do the following:</p>
  
  <pre><code>public class MyACondition : Condition&lt;AClass&gt;
  {
      public MyACondition()
      {
          // Initialize a new instance of MyACondition.
      }
  }
  </code></pre>
  
  <p>And then later inject it unwantedly like so:</p>
  
  <pre><code>AClass foo = new AClass();
  foo.SetConditions(ACondition.Not(new MyACondition()));
  </code></pre>
  
  <p>I also tried making T the return type; e.g. changing <code>Condition&lt;T&gt; And(Condition&lt;T&gt; c1, Condition&lt;T&gt; c2)</code> to <code>T And(T c1, T c2)</code>, but that forces me to inherit from <code>T</code>, which isn't allowed. So, for now, I have to write separate <code>And</code>, <code>Or</code>, and <code>Not</code> factories for each of my Condition classes. It seems like there should be a better way, but I'm stumped.</p>