1

I have an onclick event and I need to update some views on a web page according to server response. What is the best way to implement this using listeners and handlers? I think that I should create clickListener and map it to a button, but I don't know how to notify views that they should render some data. For example, I have one presenter (called Presenter) and 3 views (View1, View2, View3). And the first way is to do something like this:

public class Presenter {
    private View1 view1;
    private View3 view2;
    private View3 view3;
    private Button button;
    protected void doSmth() {
        button.setClickListener(new ClickListener() {
            view1.update();
            view2.update();
            view3.update();
        });
    }

}

But that's obviously bad approach. The good way (I think) is to do next:

public class Presenter {
    private Button button;
    protected void doSmth() {
        button.setClickListener(new ClickListener() {
            //push object that will trigger handlers in views.
        });
}
//this is the same for all views
public class View {
    protected void handleClickInPresenter() {
    //render the data
    }
}

But I can't understand how to implement this way. Ask for your help.Thanks.

Sharknado
  • 11
  • 3
  • When you ask your question, display the fact that you have done some research first; this will help establish that you're not being a lazy sponge and wasting people's time. Better yet, display what you have learned from doing [Before you ask](http://www.catb.org/~esr/faqs/smart-questions.html) research. We like answering questions for people who have demonstrated they can learn from the answers. – MikeJRamsey56 Nov 26 '16 at 14:34
  • 1
    Hi, I have changed my comment. Does it meet the guidelines now? – Sharknado Nov 26 '16 at 21:46
  • 1. You should "configure" your objects inside constructor, so this is the place where you want to set your button click listener. 2. This is classic example of observer design pattern - read about it and use it. – Piotr Sołtysiak Nov 26 '16 at 22:02
  • Can `view1`, `view2`, and `view3` all be instances of a `View` class? If so, could you define an `update` for the `View` class, and then call it on each `view` instance from the click handler? – Alessandro Scarlatti Nov 26 '16 at 22:04
  • Better. :-) [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) is always a good read for first time posters. – MikeJRamsey56 Nov 26 '16 at 23:40
  • To Piotr: thanks for an advice, haven't known about this pattern. To Alessandro: I think that it's not good way because each View has its own ViewPresenter (Presenter and View can't be bound using MVP pattern). The second big problem with this approach is extensibility. To Mike: ok, thanks, will read it. – Sharknado Nov 27 '16 at 09:48

0 Answers0