.NET and me Coding dreams since 1998!

20Oct/068

How to do multiple inheritance in C# – Implementation over delegation (IOD)

IOD is a very simple coding techniques which allows a developers fast implementation of an system interfaces and *multiple inheritance* in C# (I know:It is not supported :))


Well the whole idea is basically to have a class field of desired type and to expose functionality public properties of the class based on hidden calls to the member field


So if we have something like Class A and Class B are parents of a Class C.An example of that situation could be the case when we would have a user control which should be able to handle and present a list of customers


Illustration 1. UML Multiple inheritance diagram


Multiple inheritance in C# problem  ilustration


Because user control and List<T> are bot classes you couldn't do that directly, by inheriting, because there is no support for multiple inheritance in the case of c# (and I'm happy with that because I think multiple inheritance when not properly used very often leads to class explosion anti pattern)


Usually developers are going to solution like this one which I think is bad solution:


Illustration 2. Inherit the inherited one anti pattern


 


Our collection class would inherit user control and our own user control would inherit that *power collection class*.
Even if it will do the task of multiple inheritance in C# I don't like this solution because of the next facts:


a) Class B "the power collection" is now collection class with attributes of user controls, which is bed because all user control members are visible on collection level . What should developer of class c think about collection class which exposes width and height?


b)Class A could define some virtual members which could be overridden by class B


c)Class B couldn't be reusable in case when somebody wants to use her primarily functionality (it is a collection) but don't need the burden of a user control


d) It is very tightly coupled model.


e)It makes almost impossible inheritance of more than two parent classes


Code example


 


class CustomerCollection : System.Windows.Forms.UserControl


{


      protected virtual void Add(string customerName)


      {


            /*Customer collection add method specific code*/


      }



      protected virtual void Delete(string customerName)


      {


            /*Customer collection delete method specific code*/


      }


}


   


class MyUserControl: CustomerCollection


{


      /*MyUserControl specific code goes here*/


}


The right approach is by my opining implementation over delegation (IOD) approach which would in our case look like:


Illustration 3. Implementation over delegation multiple inheritance pattern


Implementation over delegation multiple inheritance pattern


This pattern is based on a fact that we define an interface of a class B and  than our class c is inheriting Class A directly (MyUserControl should inherit UserControl) and implements a IClassB interface.


The implementation of an interface itself is been done by keeping private member of a class B type and inside of the interface implementation methods we are just calling the method of that privately contained member.


interface ICustomerCollection


{


      void Add(string customerName);


      void Delete(string customerName);


}



class CustomerCollection : ICustomerCollection


{


      public void Add(string customerName)


      {


            /*Customer collection add method specific code*/


      }



      public void Delete(string customerName)


      {


            /*Customer collection delete method specific code*/


      }


}


   


class MyUserControl: UserControl, ICustomerCollection


{


      CustomerCollection _customerCollection=new CustomerCollection();


     


      public void Add(string customerName)


      {


            _customerCollection.Add(customerName);


      }



      public void Delete(string customerName)


      {


            _customerCollection.Add(customerName);


      }


}



Comments (8) Trackbacks (0)
  1. I'm a little confused on the term "implementation over delegation", though maybe it's just language semantics. Do you mean "over" as a synonymn to "via"?

  2. Yes, that over is in context of "via"

  3. please can u provide me concepts regarding multicast delegates??

  4. This an approximation of multiple inheritance but it really is not.  Assume you have Class A, B and C is inheriting for A and for B it does the implementation over delegation.  Well C can be cast back as A but not B.  Interesting pattern nonetheless but still not true multiple inheritance.  Just my two cents.

  5. CavenDAR obvously is 100% correct, though unfortunately till Anders decides to give us REAL MI, this is the stuff we’re stuck with…

    I wrote about it myself just now in fact at; ra-ajax.org/lsp-liskov-substitution-principle-to-be-or-not-to-be.blog

    Nice writeup, though I feel my Jedi sample is better… ;)

    • Hi Thomas,

      This is really good example of Jedi sample.
      Still it has kind of dependency
      like while instanciating jedimaster we need to know about instance of lightinghand and hence i think it is kind of dependency injection. still it does not solves the contravarience problem (i.e converting from jedimaster to lightinghand

      but in the example above we can solve the issue as explain below;
      IClassB interface = new MyUserControl();

      correct me if i am wrong since I am not master in OOPS.

      Regards,
      Mahesh M

  6. I agree – it was a nice articly, but I prefer the way Thomas Hansen illustrates it in his Jedi/Sith sample :-)

  7. Some C# coding really really really does need Mutliple inheritance – if you ever used it to full effect you will know that without doubt….

    Yes the world is safer without it – like real pointers – we should be able to decide if we use it…. surely someone will wake up?


Leave a comment

No trackbacks yet.