Search code examples
javaoopmodel-view-controllerarchitectureclass-design

OOP GUI classes designing with MVC


I have tree-based data model. I need to show it tree on tree viewer and one of element tree on element viewer like this scheme:

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

Both tree viewer and element viewer can make changes to the model.

Now I have following code:

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);
    }
}

How should be solved this task correctly from the point of view of the OOP and MVC design-pattern?


Solution

  • MVC and OOP do clash sometimes. That being said, it seems the solution could be just moving the viewer selection to the tree nodes:

    public class TreeNode {
        Viewer getViewer();
    }