oop - How to deal with Lack of Multiple Inheritance in C# -


i working on mini-framework "runnable" things. (they experiments, tests, tasks, etc.)

// "runs" (in coordinated way) multiple "runnable" things. interface irunnableof<t> : irunnable  // provide base-class functionality "runner" abstract class runnerbase<t> : irunnableof<t>   class sequentialrunner<t> : runnerbase<t>  // same interface, different behavior. class concurrentrunner<t> : runnerbase<t> // other types of runners.  class concurrentblockrunner : sequentialrunner<block> class sequentialblockrunner : concurrentrunner<block> 

now, how can reconcile concurrentblockrunner , sequentialblockrunner? mean:

  1. refer them common ancestor, use in collection. (ienuerable<t> t = ??)

  2. provide additional base class functionality. (add property, example).


i remedied #1 adding interface specified type parameter ia<t>:

interface iblockrunner : irunnableof<block> { } 

and modified concurrentblockrunner , sequentialblockrunner definitions be:

class concurrentblockrunner : sequentialrunner<block>, iblockrunner class sequentialblockrunner : concurrentrunner<block>, iblockrunner 

since concurrentblockrunner , sequentialblockrunner both use block type parameter, seems correct solution. however, can't feel "weird" it, because well, tacked interface on.


for #2, want add couple pieces of common data concurrentblockrunner , sequentialblockrunner. there several properties apply them, not common base class, way @ runnerbase<t>.

this first time while using c# i've felt multiple inheritance help. if do:

abstract class blockrunnerbase {    int prop1 { get; set; }    int prop2 { get; set; }  class concurrentblockrunner : sequentialrunner<block>, blockrunnerbase class sequentialblockrunner : concurrentrunner<block>, blockrunnerbase 

then add these properties blockrunnerbase, , work. there better way?


i know recommended consider composition, began work with:

class blockrunner : iblockrunner  {    iblockrunner _member;     int prop1 { get; set; }    // wish put these in base class    int prop2 { get; set; }            // lots of proxy calls, , proxy events _member    void method() { _member.method(); }    event someevent    {       add { _member.someevent += value; }       remove { _member.someevent -= value; }    } } 

the problem encountered (driving me write question) once compose, lose type compatibility. in case, _member firing event, sender parameter of type sequentialblockrunner. however, event handler trying cast type blockrunner, of course failed. solution there not use add/remove proxy events, handle them, , raise event of own. work add couple properties...

composition on inheritance, ftw!

to more explicit:

class sequentialrunner<t> : runnerbase<t> 

should implement irunnableof<t> , proxy runnerbase<t> without inheriting it.

class sequentialrunner<t> : irunnableof<t> {    private readonly runnerbase<t> _runnerbase;     ... } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -