title: Decorator pattern and generics
body: <p>Regular interface:</p>
  
  <pre><code>public interface IComputation
  {
      void Reset();
      float GetValue1();
      float GetValue2();  
  }
  </code></pre>
  
  <p>Generic interface:</p>
  
  <pre><code>public interface IComputation&lt;T&gt; : IComputation where T : IComputation
  {
      T Proxy { get; set; }
  }
  </code></pre>
  
  <p>Now for the classes:</p>
  
  <pre><code>public abstract class Computation&lt;T&gt; : IComputation&lt;T&gt; where T : IComputation
  {
      public T Proxy { get; set; }    
  }
  </code></pre>
  
  <p>Class 'ComputationCache' is a 'decorated" computation:</p>
  
  <pre><code>internal abstract class ComputationCache&lt;T&gt; : IComputation where T : IComputation
  {
      public T Proxy { get; set; }    
  
      public float GetValue1()
      {
          bool isCached = //check cache.
          if(!isCached)
          {
              //compute value
              float value1 = Proxy.GetValue1();                           
  
              //update cache
  
              return value;
          }       
      }
  }
  </code></pre>
  
  <p>To initialize the decorated computation, I tried the following:</p>
  
  <pre><code>public ComputationCache(IComputation&lt;T&gt; proxy)
  {
      Proxy = (T) proxy;
      proxy.Proxy = this;
  }
  </code></pre>
  
  <p>...which gives the following error":</p>
  
  <blockquote>
    <p>Cannot convert source type 'ComputationCache' to target type 'T'.</p>
  </blockquote>
  
  <p>Can someone comment on whether it is better to use:</p>
  
  <pre><code>ComputationCache&lt;T&gt; : IComputation where T : IComputation
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>ComputationCache&lt;T&gt; : IComputation&lt;T&gt; where T : IComputation
  </code></pre>