使用工厂的动态继承
我想我知道这个问题的答案,但我希望有人有一个巧妙的解决方案。我们目前使用两种下拉控件(Telerik 和.Net)。我希望将这些组合到一个控件中,但在用户友好的设计方面遇到了困难。
理想情况下,将在设计文件中创建具有 bool 属性(例如“SimpleBox”)的控件,以确定继承哪种类型的控件。然后,实例化将在代码隐藏设计文件中生成,然后构造函数将动态加载基础(这是不可能的)。对我来说,简单的解决方案是创建一个 IDropDown 接口,然后让工厂创建正确的接口。唯一真正的问题是每次都必须手动编写实例化,这很麻烦,而且根本不会加快我们的进程。
尽管这不可能直接实现,但我正在寻找一种类似于工厂的解决方案,该工厂在对象构造函数内运行,用于基于 bool 属性设置基础。
干杯
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 the base, based on a bool property.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想在这里研究组合/委托而不是继承。
本质上,不是直接扩展任一类,而是创建一个扩展
Control
(或类似的低级内容)并实现IDropDown
的包装类,添加一个IDropDown<您想要使用的底层控制实现的 /code> 字段,并将每个感兴趣的方法调用转发到选定的实现。然而,如果有很多方法,这很快就会变得乏味。
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 implementsIDropDown
, add anIDropDown
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.