gpt4 book ai didi

java - 使用 MVC 设计的 OOP GUI 类

转载 作者:行者123 更新时间:2023-11-30 07:18:10 25 4
gpt4 key购买 nike

我有基于树的数据模型。我需要在树查看器上显示它树,并在元素查看器上显示元素树之一,如下方案:

tree viewer | element viewer:
Element A | There should be content from selected element (Element C).
-Element B |
-Element C |
(Selected)|

树查看器和元素查看器都可以对模型进行更改。

现在我有以下代码:

public class TreeNode{...};
public class AClass extends TreeNode{...};
public class BClass extends TreeNode{...};
public class CClass extends TreeNode{...};

public class Viewer{
...
//Specifies how to display data from a treeNode
public abstract void showModel();
...
};
public class AViewer extends Viewer{...};
public class BViewer extends Viewer{...};
public class CViewer extends Viewer{...};

public class Controller{
...
public void chooseViewer(TreeNode treeNode ){
if (treeNode instanceof AClass) elementViewer = new AViewer(treeNode);
else if (treeNode instanceof BClass) elementViewer = new BViewer(treeNode);
else if (treeNode instanceof CClass) elementViewer = new CViewer(treeNode);
}
}

从OOP和MVC设计模式的角度来看,应该如何正确地解决这个任务?

最佳答案

MVC 和 OOP 有时确实会发生冲突。话虽这么说,解决方案似乎只是将查看器选择移动到树节点:

public class TreeNode {
Viewer getViewer();
}

关于java - 使用 MVC 设计的 OOP GUI 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38050063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com