显示基于具体对象类型的动态 UI?
我有一个基类和几个从它派生的具体类。我们可以说它看起来像这样:
abstract class Base
{
public int id;
public string baseVariable;
}
class ConcreteA extends Base
{
public string propertyA;
public float propertyB;
}
class ConcreteB extends Base
{
public decimal propertyC;
public RandomClass propertyD;
}
我们可以说 UI 是一个很大的开放空间,其中漂浮着一堆从 Base 派生的具体对象。在此视图中,您需要的只是常规 id
和 baseVariable
属性,以获得显示通用对象所需的内容。如果双击其中一个对象,则会弹出一个窗口,您可以在其中编辑基础对象和混凝土对象的所有属性。
在该窗口中,我希望根据我拥有的具体对象类型动态加载选项卡,以及用于修改常见 Base
属性的静态加载选项卡。是否有一个好的设计模式来实现这样的事情?
看来我应该避免这种情况:
class PopUpWindow
{
public PopUpWindow(object:Base)
{
tabs.add(BaseTab);
switch(typeOf(object))
{
case ConcreteA:
tabs.add(ConcreteATab);
case ConcreteB:
tabs.add(ConcreteBTab);
}
}
}
但我也不认为我应该像这样在对象本身中混合 UI 内容:
public PopUpWindow(object:Base)
{
tabs.add(BaseTab);
tabs.addAll(object.TabsToShowOnEdit);
...
有什么想法或建议吗?抱歉问了这么长的问题。提前致谢。
I have a base class and several concrete classes that derive from it. Lets just say it looks like this:
abstract class Base
{
public int id;
public string baseVariable;
}
class ConcreteA extends Base
{
public string propertyA;
public float propertyB;
}
class ConcreteB extends Base
{
public decimal propertyC;
public RandomClass propertyD;
}
Lets just say the UI is a big open space with a bunch of concrete objects that derive from Base floating around in it. In this view all you would need is the general id
and baseVariable
properties to get what you need to display the generic objects. If you double click on one of the objects, it would then pop up a window where you could edit all the properties of the Base and Concrete objects within.
In that window, I want to have dynamically loaded tabs based on which type of concrete object I have, and statically loaded tabs that are used to modify common Base
properties. Is there a good design pattern for implementing something like this?
It seems like I should avoid this:
class PopUpWindow
{
public PopUpWindow(object:Base)
{
tabs.add(BaseTab);
switch(typeOf(object))
{
case ConcreteA:
tabs.add(ConcreteATab);
case ConcreteB:
tabs.add(ConcreteBTab);
}
}
}
But I also don't think I should be mixing UI stuff in the objects themselves like so:
public PopUpWindow(object:Base)
{
tabs.add(BaseTab);
tabs.addAll(object.TabsToShowOnEdit);
...
Any ideas or suggestions? Sorry for the long winded question. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是拥有一个工厂,您可以在其中根据模型类型注册视图类型:
然后您可以在填充 PopupWindow 时轻松使用该工厂:
One way is to have a factory in which you register view types based on model type:
Then you can easily use the factory when populating your PopupWindow: