1

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.

tucuxi
  • 17,561
  • 2
  • 43
  • 74
Meho
  • 13
  • 5

2 Answers2

1

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).

tucuxi
  • 17,561
  • 2
  • 43
  • 74
0

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 :-)

Community
  • 1
  • 1
Meho
  • 13
  • 5