1

I'm working on android with Java and I'm implementing the Model-View-Presenter architecture. There are two types of games the player can play:

  • Game A
  • Game B

Both games are really similar but with each their respective .class documents and (e.g. GameA.class and a GameB.class).

In both cases, their respective presenters are the same, with the only thing changing being the instantiation & declaration of the model class. For example:

GameAPresenter.class:

class GameAPresenter{

    private GameA game;
    // other stuff here that happens in both presenters

    GameAPresenter(int par1, int par2){
        this.game = new GameA(par1, par2);
        //other stuff here that happens in both presenters

    }
}

GameBPresenter.class:

class GameBPresenter{

    private GameB game;
    // other stuff here that happens in both presenters

    GameBPresenter(int par1, int par2){
        this.game = new GameB(par1, par2);
        //other stuff here that happens in both presenters

    }
}

Is there any way I can cleanly avoid having duplicate code, simulated by the single-line comments? Bonus if I can make both models share the one presenter.

Andrii Artamonov
  • 622
  • 8
  • 15
g_elef
  • 57
  • 9

1 Answers1

2

You are gonna want to create a generic Game class that GameA and GameB can then both inherit from.

Same can go with the GamePresenter, create a generic one that GamePresenterA and GamePresenterB can inherit from. Also you can give the GamePresenter a Game everytime you create a new instance of it or call a certain method. That way there can be a single GamePresenter and it can take whatever Game to present it.

Nizar
  • 2,034
  • 1
  • 20
  • 24
  • 3
    Consider whether `Game` should be an interface instead of a superclass. Also, `Game` doesn't need to be generic here, and `GamePresenter` may not need to be generic either; polymorphism is probably enough. It may turn out that only one `GamePresenter` class is needed after all. – kaya3 Jan 15 '20 at 19:47
  • Alright, thanks a lot [kaya3](https://stackoverflow.com/users/12299000/kaya3), to be honest, I am not great with the technical terms. Thanks for the recommendation :D – Nizar Jan 15 '20 at 19:49
  • 1
    To add to @kaya3's comment. We tend to [prefer composition over inheritance](https://stackoverflow.com/q/49002/42962). – hooknc Jan 15 '20 at 19:51
  • Sorry guys, but I really want to expand my knowledge in this area, is this mainly design patterns and software engineering ? – Nizar Jan 15 '20 at 19:54
  • 1
    This fits perfectly into the abstraction I was going to create tomorrow when I'll be refactoring my code. Thank you for the great answer Nizar! – g_elef Jan 15 '20 at 19:56
  • 2
    @Nizar This is definitely about OO design, yes; I'd call it a more general design issue than one about design patterns specifically, but perhaps there is a named pattern which applies in this case. Design is often considered a part of software engineering, yes. – kaya3 Jan 15 '20 at 21:20