使用MVC进行OOP GUI类设计

时间:2016-06-27 09:14:56

标签: java oop model-view-controller architecture class-design

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

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设计模式的角度来看,如何正确解决这个问题呢?

1 个答案:

答案 0 :(得分:1)

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

public class TreeNode {
    Viewer getViewer();
}