Java 有没有一种方法可以在这个实例中完全避免重复的代码?

Java 有没有一种方法可以在这个实例中完全避免重复的代码?,java,android,class,duplicates,mvp,Java,Android,Class,Duplicates,Mvp,我正在用Java开发android,并正在实现模型视图演示器体系结构。玩家可以玩两种类型的游戏: 游戏A 游戏B 这两款游戏非常相似,但都有各自的.class文档和(例如GameA.class和GameB.class) 在这两种情况下,它们各自的演示者是相同的,唯一改变的是模型类的实例化和声明。例如: GameAPresenter.class: class GameAPresenter{ private GameA game; // other stuff here tha

我正在用Java开发android,并正在实现模型视图演示器体系结构。玩家可以玩两种类型的游戏:

  • 游戏A
  • 游戏B
这两款游戏非常相似,但都有各自的.class文档和(例如GameA.class和GameB.class)

在这两种情况下,它们各自的演示者是相同的,唯一改变的是模型类的实例化和声明。例如:

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

    }
}
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

    }
}
GameBPresenter.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

    }
}
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

    }
}
有没有什么方法可以完全避免使用单行注释模拟的重复代码?
如果我可以让两个模型共享一个演示者,那就有好处了。

您需要创建一个通用的
Game
类,该类
GameA
GameB
都可以继承


同样可以使用
GamePresenter
,创建一个通用的
GamePresenterA
GamePresenterB
可以继承的。此外,每次创建新实例或调用某个方法时,您都可以为
GamePresenter
提供
Game
。这样就可以有一个单一的代码> GamePresenter < /Code >,它可以使用任何游戏来呈现它。

代码审查的问题应该被问到:<代码>游戏是否应该是一个接口而不是一个超类。另外,
Game
在这里不需要是通用的,
GamePresenter
也可能不需要是通用的;多态性可能已经足够了。可能最终只需要一个
GamePresenter
类。好吧,非常感谢,老实说,我对技术术语不太在行。感谢您的推荐:DTo添加到@kaya3的评论中。我们倾向于这样做。这完全符合我明天重构代码时要创建的抽象。谢谢你的回答,尼扎尔@Nizar这绝对是关于OO设计的,是的;我将其称为一个更一般的设计问题,而不是一个关于设计模式的问题,但可能有一个命名模式适用于本例。是的,设计通常被认为是软件工程的一部分。