How to reuse code when multiple inheritance is not

2019-04-14 16:36发布

I would like to make use of few methods from couple of my old tested classes into my new class that I am building. Unfortunately, C# does not support multiple inheritance. How do I reuse code from these old classes? Do I just create them as member objects? or Do I have any other options?

6条回答
Melony?
2楼-- · 2019-04-14 16:53

Using them as member objects should be a good idea. Then you can also expose only the methods of interest, and adapt them if necessary.

查看更多
Rolldiameter
3楼-- · 2019-04-14 16:56

You can fake it pretty easily like this:

public interface IFoo {
    void DoFoo();
}

public class Foo : IFoo {
    public void DoFoo() { Console.Write("Foo"); }
}

public class Bar {
    public void DoBar() { Console.Write("Bar"); }
}

public class FooBar : IFoo, Bar {
    private IFoo baseFoo = new Foo();
    public void DoFoo() { baseFoo.DoFoo(); }
}

//...

FooBar fooBar = new FooBar();

fooBar.DoFoo();
fooBar.DoBar();
查看更多
贪生不怕死
4楼-- · 2019-04-14 16:59

Generally, using composition instead of inheritance is the way forward, yes. If you could give a concrete example of the kind of thing you mean, that would make it easier to help find the appropriate approach though: it's not always the same.

查看更多
一夜七次
5楼-- · 2019-04-14 16:59

You can recreate then as extension methods when they work on the same type.

public static void MyMethod( this MyType target ){}


MyType.MyMethod()

as noted below, if you have a class which derives from MyType or more common implements the interface the extension method works on, the extension works for those.

public class MyDerived : MyType{}

MyDerived.MyMethod()
查看更多
Luminary・发光体
6楼-- · 2019-04-14 16:59

Technically, C# implements multiple interface inheritence, but not multiple implementation inheritence.

The problem with multiple implementation inheritence is that it leads to situations like the diamond problem.

You can simulate multiple implementation inheritence using multiple interface inheritence and composition, which is close to what you described as creating the objects that you want to re-use "as member objects". What you don't want to do, however is expose unnecessary behaviour from your wrapped type.

Following is a class diagram showing the basic principle of multiple inheritence through composition:

Multiple Inheritance http://www.freeimagehosting.net/uploads/cb219cefba.jpg

The drawback of this is that any changes to the interface IBreeper will require corresponding changes to Breeper, and vice versa.

Taking a step back though - you need to carefully look at the reasons for composing the old classes. Is it to:

  1. Enhance or step up existing behaviour. You should then consider the Proxy Pattern;

  2. Create a "one stop shop" for all of your behaviour. You should really only be doing this if your SuperFuddler is cohesive (that is, all exposed behaviour is aligned), otherwise you'll be moving into a God Object situation which isn't conducive to being maintainable;

  3. Present an interface that is more consistent with a mode of use required by a certain class of clients of your SuperFuddler. In this case, you should consider the Adapter Pattern.

查看更多
神经病院院长
7楼-- · 2019-04-14 17:03

if you have developed them as a component , use them dont inherite from them

查看更多
登录 后发表回答