Reputation: 3509
I've got an issue with the non-allowed multiple inheritance in C#
Following scenario:
class MasterCamera { ... }
class CameraFromManufacturerA : MasterCamera { ... }
class CameraFromManufacturerB : MasterCamera { ... }
class CameraFromManufacturerC : MasterCamera { ... }
MasterCamera
provides some functionality like StartCamera()
, ConnectCamera()
, etc.
In my main code I use an object MasterCamera mCamera = CameraSelector.GetCamera();
with CameraSelector
checking whether a camera from A, B or C is connected, and returns that object (e.g. CameraFromManufacturerA
)
This works perfectly fine in theory, but one of these camera's API needs to use the WindowsMessageCallback (WndProc
), thus I need to include for that camera only Windows.System.Forms.Form
Since C# does not allow multiple inheritance, and afaik Form has no interface class, I have to make MasterCamera
inherit from Windows.System.Forms.Form
I do not want that, so how can I go around that?
Upvotes: 1
Views: 107
Reputation: 21241
This is a common problem with inheritance, which is why the GoF state "Favour composition over inheritance"
You could use the Strategy pattern. Instead of deriving different types of MasterCamera
, instead derive different types of ICameraFunctionality
, and store an object of that instance in your MasterCamera
(or rather just Camera
) class.
That way MasterCamera just does the common work, but calls its ICameraFunctionality
instance to perform specific work.
Upvotes: 2
Reputation: 13872
You are trying Inheritance
. Give a try to Composition
also.
Can't Camera
be contained in a Form
?
Upvotes: 1
Reputation: 697
You shouldn't use MasterCamera { ... } as a Class but use it as Interface like:
public interface MasterCamera { ... }
and it must work.
Upvotes: 2
Reputation: 62248
What if you simply invert dependency in your code.
Create a Form
class that overrides WindowsMessageCallback
and is able to host MasterCamera
object, which would be that child Camera
class that you need.
Upvotes: 1