Creating passive presentations for children

I am very interested in using a passive view template to improve testability, but I'm not sure how to call child dialogs.

Do you have a parent view that creates a child view and returns an interface to the parent controller, and then the parent controller creates the child controller?

+4
source share
1 answer

I thought a lot about this, and I think I have several possible solutions.

  • Add a method to the view class to create a subordinate dialog
  • Create a factory that generates the model, view. and controller for subordinate dialogue

Method 1

class ParentView extends JDialog implements IParentView { public IChildView newChildView(...) { return new ChildView(...); } // ... } interface IParentView { IChildView newChildView(...); // ... } class ParentController { private IParentView view; public ParentController(IParentView view) { this.view = view; } public void showChildView() { IChildView childView = view.newChildView(); ChildController childController = new ChildController(childView); childView.setVisible(true); } } class ChildView extends JDialog implements IChildView { // ... } interface IChildView { void setVisible(boolean visible); } class ChildController { private IChildView view; public ChildController(IChildView view) { this.view = view; } } 

Method 2

 // During testing, create a mock ChildFactory and assign it to instance class ChildFactory implements IChildFactory { private static IChildFactory instance; public static ChildFactory getInstance() { if (instance == null) { instance = new ChildFactory(); } return instance; } public static void setInstance(IChildFactory factory) { instance = factory; } public void createChild(IParentView parent) { IChildView view = new ChildView(parent); ChildController controller = new ChildController(view); view.setVisible(true); } } interface IChildFactory { void createChild(IParentView parent); } class ParentController { private IParentView view; public ParentController(IParentView view) { this.view = view; } public void showChildView() { ChildFactory.getInstance().createChild(view); } } // ParentView class similar to method 1 
+1
source

Source: https://habr.com/ru/post/1301148/


All Articles