[MVP] - One Presenter for each View

Hi everybody,

i need some help with the mvp pattern which i´m using in my application for the first time.
is there one big presenter which “knows” all the views and the model
or
is there a presenter for each view?

Thanks for your help

Hi,

typically there’s a dedicated presenter for each view - otherwise you could quickly get a huge presenter which would not be maintainable or very clear.

-tepi

ok thank you.

then i was right and my coworker wrong :smiley:

another short question:

i´m using the viewprovider class from the dashboard example:

@SuppressWarnings("serial")
public class DashboardNavigator extends Navigator {

    private static final DashboardViews ERROR_VIEW = DashboardViews .SOURCE;
    private ViewProvider errorViewProvider;

    public DashboardNavigator (final ComponentContainer container) {
        super(UI.getCurrent(), container);

        initViewChangeListener();
        initViewProviders();

    }

    private void initViewChangeListener() {
        addViewChangeListener(new ViewChangeListener() {

            @Override
            public boolean beforeViewChange(final ViewChangeEvent event) {
                // Since there's no conditions in switching between the views
                // we can always return true.
                return true;
            }

            @Override
            public void afterViewChange(final ViewChangeEvent event) {
                DashboardViews view = DashboardViews.getByViewName(event.getViewName());
                // Appropriate events get fired after the view is changed.
                MyEventBus.post(new PostViewChangeEvent(view));
                MyEventBus.post(new BrowserResizeEvent());
                MyEventBus.post(new CloseOpenWindowsEvent());

            }
        });
    }

    private void initViewProviders() {
        // A dedicated view provider is added for each separate view type
        for (final DashboardViews viewType : DashboardViews.values()) {
            ViewProvider viewProvider = new ClassBasedViewProvider(viewType.getViewName(), viewType.getViewClass()) {

                // This field caches an already initialized view instance if the
                // view should be cached (stateful views).
                private View cachedInstance;

                @Override
                public View getView(final String viewName) {
                    View result = null;
                    if (viewType.getViewName().equals(viewName)) {
                        if (viewType.isStateful()) {
                            // Stateful views get lazily instantiated
                            if (cachedInstance == null) {
                                cachedInstance = super.getView(viewType
                                        .getViewName());
                            }
                            result = cachedInstance;
                        } else {
                            // Non-stateful views get instantiated every time
                            // they're navigated to
                            result = super.getView(viewType.getViewName());
                        }
                    }
                    return result;
                }
            };

            if (viewType == ERROR_VIEW) {
                errorViewProvider = viewProvider;
            }

            addProvider(viewProvider);
        }

        setErrorProvider(new ViewProvider() {
            @Override
            public String getViewName(final String viewAndParameters) {
                return ERROR_VIEW.getViewName();
            }

            @Override
            public View getView(final String viewName) {
                return errorViewProvider.getView(ERROR_VIEW.getViewName());
            }
        });
    }
}

the views are created in the “getView” method. but where should i add my presenter implementation?
Normaly i solve this problem like this: (https://vaadin.com/book/-/page/advanced.architecture.html)

// Create the model and the Vaadin view implementation
CalculatorModel    model = new CalculatorModel();
CalculatorViewImpl view  = new CalculatorViewImpl();
    
// The presenter binds the model and view together
new CalculatorPresenter(model, view);
    
// The view implementation is a Vaadin component
layout.addComponent(view);

anybody?!

Well, it can be done in a lot of various ways. The way I usually do it is that when you instantiate the view, it will create it’s own presenter and hook the view instance to it.

hmm ok thats one way to solve this problem but i want a way where the view doesnt know the presenter