MVP createing of child view

39 views Asked by At

I have a question about the MVP pattern. I have parent view that on button click creates a new view at runtime. Each view has its own presenter and both of them have access to the same model class.

  1. Where should the creation of the new child view be? in the parent presenter or in the parent view?

  2. How can the child view access the model? Does it get the model from the parent presenter that is being used in the parent view?

  3. Should the model be only a POJO or can it contain business logic and access the DB through DAOs? If this is the case what business logic goes in the presenter?

I am want to achieve a passive view so that all the logic can be tested.

Here is a code sample:

interface IViewA {
    buildView();    
    ... other methods...
}

class ViewA implements IViewA{

    IPresenterA ipresenterA;
    
    public(IPresenterA ipresenterA){
        this.ipresenterA = ipresenterA;
        this.presenterA.setView(this);
    }

    @Override
    void buildView(){

        button.onClick(clickEvent ->{
            //ipresenterA.handleClickEvent(clickEvent);
            
            //create the new view here instead of in the presenter?
            IViewB viewB = new ViewB(new PresenterB(ipresenterA.getModel()));
            showWindow(viewB);
        });
    }   
}


interface IPresenterA{
    void handleClickEvent();
    
    Model getModel();
    
    setView(IViewA iViewA);
}


class PresenterA implements IPresenterA{

    Model model;
    IViewA iViewA;
    
    public PresenterA(Model model){
        this.model = model;
    }

    @Override
    void handleClickEvent(){
        var data = model.getData();
        ... create the new view with data....
    }
    
    @Override
    Model getModel(){
        return this.model;
    }
    
    @Override
    void setView(IViewA iViewA){
        this.iViewA = iViewA;
    }
}

interface IViewB {
    ...
}


class ViewB implements IViewB{

    IPresenterB ipresenterB;
    
    public(IPresenterB ipresenterB){
        this.ipresenterB = ipresenterB;
        this.ipresenterB.setView(this);
    }
    
    
}

interface IPresenterB{
    Model getModel();
    
    void setView(IViewB iViewB);
    
    void getDataFromDb();
}


class PresenterB implements IPresenterB{

    Model model;
    IViewB iViewB;
    
    public PresenterB(Model model){
        this.model = model;
    }

    @Override
    Model getModel(){
        return this.model;
    }
    
    @Override
    void setView(IViewB iViewB){
        this.iViewB = iViewB;
    }
    
    
    @Override
    void getDataFromDb(){
        var data = this.model.getData();
        ...
    }
    
    ...
}

class Model{

    public Data getData(){
        ...access the DB...
    }
    ...other logic...
}


clas Client{
    
    main(){
        Model model = new Model();
                
        IPresenterA ipresenterA = new PresenterA(model);
        IViewA iViewA = new ViewA(ipresenterA);
        
        showView(iViewA);
        ...
    }

}

Thank you!

0

There are 0 answers