I feel like having lost my OO knowledge. Actually, the problem looks simple: I want to create a class which operates on an object which
at the same time.
I could create a new Form type public abstract class SpecialForm : Form, IDescribedActionsProvider and require a parameter of that SpecialForm type.
But it does not work here: most of our Forms inherit from OurCompanyForm which need not implement IDescribedActionsProvider, and some of our forms do not inherit therefrom, but when they implement the interface, they should be an acceptable parameter. It is composition what is required here, not inheritance.
Then I thought of Generics. I could define my class like
public class FormController<T> : IDisposable where T : Form, IDescribedActionsProvider
{
T _ControlledForm;
OtherType _OtherObject;
internal FormController(OtherType otherObject, T controlledForm)
{
_OtherObject = otherObject;
_ControlledForm = controlledForm;
....
}
It would work the way I intended, but it feels totally wrong: That's not what Generics (see MSDN at http://msdn.microsoft.com/en-us/library/sz6zd40f%28v=vs.100%29.aspx) were meant for. And I get problems when I want to create that FormController object in a Factory (because of the OtherType which is created there) which is not generic, i.e. a function like public FormController CreateFormcontroller(T controlledForm) is not possible because T not defined here... And using "SpecialForm" instead of T does not work because of composition vs. inheritance.
What do you suggest? "Duck Typing" - but that's not a good OO practise either?
Edits:
The object to be passed to FormController must be of both types, because it describes to events of Form and calls methods of the interface.
Defining T in the factory method as described in Boris B.'s comment and Weyland Yutani's answer works.
But still I have doubts about the use of Generics in this context: The T controlledForm parameter is a single object, there is exactly only one instance of that "type" in the FormController class, and nowhere does the concrete type matter - that's clearly different from e.g. a List<int>. I do not "need" a FormController object, just a FormController (which happens to work on that form).
"And I get problems when I want to create that FormController object in a Factory (because of the OtherType which is created there) which is not generic, i.e. a function like public FormController CreateFormcontroller(T controlledForm) is not possible because T not defined here..."
I might have got the wrong end of the stick but can't you just define T on the factory method?
class Factory
{
public static FormController<T> CreateFormController<T>(T controlledForm) where T : Form, IDescribedActionsProvider
{
return new FormController<T>(new OtherType(), controlledForm);
}
}
class FormController<T> where T : Form, IDescribedActionsProvider
{
T _ControlledForm;
internal FormController(OtherType otherObject, T controlledForm)
{
_ControlledForm = controlledForm;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With