开发者

Dynamic Inheritance using a factory

开发者 https://www.devze.com 2023-01-19 09:00 出处:网络
I think I know the answer to this, but I\'m hoping someone has a neat solution. We are currently using two kinds of drop down controls (Telerik and .Net). I\'m hoping to combine these into one control

I think I know the answer to this, but I'm hoping someone has a neat solution. We are currently using two kinds of drop down controls (Telerik and .Net). I'm hoping to combine these into one control, but struggling with a user friendly design.

Ideally, the control would be created in the design file with a bool property of, say, "SimpleBox," to determine which kind of control to inherit. The instantiation would then be generated in the code-behind design file and the constructor would then dynamically load the base (which isn't possible). The easy solution would be for me to create an IDropDown interface, then have a factory create the correct one. The only real problem with this is the fact that the instantiation has to be manually written every time, which is a hassle, and does not speed up our process at all.

Although it isn't directly possible, I'm looking for a solution along the lines of a factory which is run inside the object constructor for setting 开发者_StackOverflow中文版the base, based on a bool property.

Cheers


You might want to look into composition/delegation instead of inheritance here.

In essence, rather than extending either class directly, create a wrapper class that extends Control (or something similarly low-level) and implements IDropDown, add an IDropDown field for the underlying control implementation you want to use, and forward every method call of interest to the selected implementation. This rapidly becomes tedious if there are a lot of methods, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消