-3

What is the best solution for this problem?

This is the superClass

public class Views<T> {

private T controller;

public Views() {
}

public Stage getStage(Node object){
    return (Stage) object.getScene().getWindow();
}

public T getController(){
    return controller;
}

public void setController(T controller){
    this.controller = controller;
}

}

I have this interface:

public interface UserView extends Observer{
    public void setLoginObjects(Parent root);
    public void loginStartUI() throws Exception;

    public void setCriarUserObjects(Parent root, User user);
    public void criarUserStartUI(User user) throws Exception;
}

I have this subclass of Views:

public class UserViewImpl extends Views<UserController> implements UserView{
...

And i have this class:

public class UserController {

private UserView view;
private UserDAOImpl model;

public UserController(UserView view, UserDAOImpl model) {
    this.model = model;
    this.view = view;
    view.setController(this);
    this.model.addObserver(view);
}
...

What is the best solution for this problem? Should i put together Views and UserView? (The problem for this solution is i will have more views, for example i have ParqueView and ParqueViewImpl, that basically will have the same methods as Views, so there would be repeated code and i want to avoid that)

The main problem is that inside my UserController i pass as parameter UserView, which does not have access to super class methods, and i need to access setController which is located in Views class, it doesnt make sense to change the type of UserView to Views in userController because of the MVC

Hugo Modesto
  • 63
  • 1
  • 8
  • 2
    "What is the best solution for this problem?" - You never explained what the problem is (the title does not suffice). – Jacob G. Dec 31 '18 at 18:53
  • Would `ParqueView` also have/need the methods from `UserView`? – Thiyagu Dec 31 '18 at 18:55
  • @user7 no, ParqueView needs the methods from Views – Hugo Modesto Dec 31 '18 at 18:56
  • @JacobG. sorry i though the problem was pretty obvious, ill edit the post! – Hugo Modesto Dec 31 '18 at 18:56
  • You could make View an interface and make `UserView` implement it? – Thiyagu Dec 31 '18 at 19:01
  • What precisely are you seeking to achieve by using generics? – Joe C Dec 31 '18 at 19:03
  • @user7 hmm, it might work because then UserView would have the method that i want to access, but i create class Views to avoid code repetition :/ – Hugo Modesto Dec 31 '18 at 19:04
  • @JoeC i use generics because, for UserViewImpl i will have UserController, for ParqueViewImpl i will have ParqueController – Hugo Modesto Dec 31 '18 at 19:05
  • Might sound crazy - You can have one class called `ObservableView` that implemetns View and extends Observer and make `UserView` extend that. But I don't want to solve a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here. We need to understand the class model to understand more. – Thiyagu Dec 31 '18 at 19:08

1 Answers1

0

It looks like you do not need generics here. Simply using an interface should suffice.

Create a Controller interface, which your two controllers will implement. Then you can get rid of the <T> entirely and use this interface instead.

Joe C
  • 15,324
  • 8
  • 38
  • 50