c# - Decorator pattern and generics -


regular interface:

public interface icomputation {     void reset();     float getvalue1();     float getvalue2();   } 

generic interface:

public interface icomputation<t> : icomputation t : icomputation {     t proxy { get; set; } } 

now classes:

public abstract class computation<t> : icomputation<t> t : icomputation {     public t proxy { get; set; }     } 

class 'computationcache' 'decorated" computation:

internal abstract class computationcache<t> : icomputation 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;         }            } } 

to initialize decorated computation, tried following:

public computationcache(icomputation<t> proxy) {     proxy = (t) proxy;     proxy.proxy = this; } 

...which gives following error":

cannot convert source type 'computationcache' target type 't'.

can comment on whether better use:

computationcache<t> : icomputation t : icomputation 

vs

computationcache<t> : icomputation<t> t : icomputation 

first, should use:

computationcache<t> : icomputation<t> t : icomputation 

if want use cache icomputation<t> , have access proxy property.

second, error:

cannot convert source type 'computationcache' target type 't'.

just this:

proxy.proxy = (t)(icomputation)this; 

(i don't know why there error. since computationcache icomputation, t...)


edit: found makes solution wrong:

new computationcache<computation<icomputation>>(new computation<computation<icomputation>>()); 

this post helped find error generics variance

t becomes computation<icomputation> isn't equal computationcache type.

you have troubles make working proxy property wrote it.

you may want then:

public interface icomputation {     icomputation proxy { get; set; }     // other stuff }  public interface icomputation<t> : icomputation t : icomputation {     new t proxy { get; set; }     // other stuff } 

but have manage 2 properties proxy.

i don't know trying achieve, it's start.


Comments