Searching for an example of class-binding by a interface

46 views Asked by At

I am trying try to implement a MVP-Pattern example in Java but I do not know how the interface connection between the Presenter and the View works! Does someone know a good example of this?

More details: In some sources a class diagram looks like this diagram

The arrow between the Presenter and the View is interrupted by a ball. This is the symbol of a interface, right?

The Presenter knows the View and the View knows the Presenter, so both need references to each other. For testing I don't want to write new ..(); in the constructor.

If I set the View- and the Presentor- reference by constructor it looks like
this:

CentralView myView = new CentralView(myPresenter);
CenterPresenter myPresenter = new CenterPresenter(myView); 

I would appreciate an example of how this works, without "new" in constructor, to be testable and without getter and setter.

2

There are 2 answers

0
tucuxi On BEST ANSWER

I find this easiest:

 Model model = new Model();
 View view = new View();
 Presenter presenter = new Presenter(model, view);
 view.setPresenter(presenter);

However, if you insist on "no setters", you should really look up on dependency injection. For example, using guice:

 // can resolve dependencies by itself
 Presenter presenter = new Presenter();

 // Dependency injection hard at work within your constructor
 @Inject
 Presenter(Model model, View view) {
      this.model = model;
      this.view = view;
 }

Dependency injection can be used both to replace factories and to resolve circular dependencies (guice uses "proxies" for that).

0
Meho On

i founded an example, to my question, here: MVP-Example with interfaces It use setter and getter, but it explain how it works with interfaces.

Bye :-)