Design pattern to use instead of multiple inheritance?

You can use the strategy pattern or something like it to use has a (composition) instead of is a (inheritance): public class TypeA : CustomButtonUserControl, IMagician { IMagician magicObj = new Magical(); public void DoMagic() { magicObj.DoMagic(); } } public class TypeB : CustomButtonUserControl, IMagician { IMagician magicObj = new Magical(); public void DoMagic() { magicObj.DoMagic(); } } public class Magical : IMagician { public void DoMagic() { // shared magic } } There are other ways to instantiate your private IMagician members (such as passing them as a param via constructor) but the above should get you started.

In . Net, you can have extension methods apply to interfaces. It's really neat when it's possible, and applicable for you because it's a rare way to apply a common implementation to an interface.

Certainly consider it, but it might not work for you since you say that DoMagic works with a lot of Private members. Can you package these private variables internal possibly? This way the extension method could access them.

Have the common functionality in another class. If there's a logical place to put this common functionality, pass your objects to this other class method (perhaps this is UI functionality, and you already have a UI helper . .. ).

Again, can you expose the private data with an internal/public property?(Security/encapsulation is a concern in all this of course. I don't know if your classes are for internal use only or will be exposed publicly. ) Otherwise, pass a separate functionality class (or specific function pointer) into the interface-defined method.

You would have to have a little bit of duplicated code to pass your private variables to this external function reference, but at least it wouldn't be much, and your implementation would be in one place. We might be making this too complicated. It won't make you feel all object-oriented when you go to sleep tonight, but could you have a static routine in your library somewhere that all IMagician implementers call?

In the end, Adapter might indeed be what you're looking for. Less likely but still worth consideration is the Decorator pattern. If nothing seems particularly good, pick what feel best, use it a couple times, and rearrange tomorrow.

:).

Your gut is correct in this case. The Adapter pattern is what you're looking for. DoFactory has good .

NET examples (that should be pretty close to their Java counterparts as well): Adapter Design Pattern in C# and VB.NET.

Replace inheritance with composition. Move your 'common' function to separate class, create an instance of that class, and insert it to TypeA object and to TypeB object.

} public static class MyExtensions { public static void doMagic(this IMagician obj) { // do your magic here } } Now, the problem is if you REALLY need to use private properties/methods (as opposed to "internal" ones), this approach won't work. Well, actually, you may be able to do your magic if you can read those properties through reflection, but even if it works, it's a rather ugly solution :) Note that "doMagic" will automatically appear to become a part of TypeA and TypeB,simply because you implement IMagician - there is no need to have any implementation there .

The composite pattern is meant for complex objects, that means the focus is on one object being made up of other objects. The strategy-pattern can be regarded as a special case of that, but a strategy does not have to be an object. I think this would apply more to your case.

Then again, this heavily depends on the nature of what DoMagic() does.

You can use composition to have magician as a property of typeA and typeB class Magician : IMagician { public void DoMagic() {} } Class TypeA : CustomButtonUserControl { //property Magician magicianInTypeA } Class TypeB : CustomTextUserControl { //property Magician magicianInTypeB }.

Abstract class Magical: CustomButtonUserControl { public void DoMagic() { // ... } } public class TypeA : Magical { } public class TypeB : Magical { }.

This is precisely what I would do by instinct. Sadly, CustomButtonUserControl and CustomTextUserControl are way to different to make this work, hence my question. Or am I missing something here?

Its hard to judge your answer when you only post code. – mizipzor Mar 25 '10 at 14:41 2 OP said specifically: I cant substitute the base class. – Arne Sostack Mar 25 '10 at 14:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions