HiveBrain v1.2.0
Get Started
← Back to all entries
principlejavaMinor

Strategy Game Menus and OOP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
menusgamestrategyandoop

Problem

Most of my experience is with Objective-C, so I am relatively new to Java inheritance. I understand that there are concepts such as abstract classes and interfaces, but I am still not totally sure when to use them. I would really like feedback on the structure of my inheritance in the following code. I am most concerned about the readability of the code, and following Java best practices.

Here I have the code for the menus of my game. Typically, when a user presses a key or clicks a button, the menu will appear on screen. I have a number of different types of menus that have different buttons and display different information. I should note that I am using libGDX Tables for the layout of the menus.

First, here is the base class that all the other menu classes will subclass:

BZMenuTable

public class BZMenuTable {

    protected Table menuTable;
    protected final Skin skin;
    private final Stage hudStage;
    private boolean isOpen;

    public BZMenuTable(Skin skin, Stage hudStage) {
        this.skin = skin;
        this.hudStage = hudStage;
        this.isOpen = false;
    }

    public boolean isOpen() {
        return this.isOpen;
    }

    public void open() {
        this.isOpen = true;
        this.menuTable = new Table(this.skin);
        this.hudStage.addActor(this.menuTable);
    }

    public void build() {
        //overridden by subclasses
    }

    public void close() {
        this.isOpen = false;
        this.menuTable.remove();
    }

}


Next I will provide two different examples of menu subclasses. This first one has pages:

HelpMenu

```
public class HelpMenu extends BZMenuTable {

private int pageNumber;
private HashMap> pageContent = new HashMap>();
private final NinePatch menuBackground;
private final float stageWidth;
private final float stageHeight;

public HelpMenu(Skin skin, Stage hudStage, NinePatch menuBackground, float stageWidth, float stageHeight) {
super(skin, hudStage);
t

Solution

I'll focus my review on this small piece of code :

public void build() {
    //overridden by subclasses
}


This tell me that your class BZMenuTable should be an abstract class. If this is only implemented in the subclass, that means you never really need BZMenuTable, other than to provide method that need to be there for all the inherited classes.

Since you do implement some method in the parent class, that means it's not an interface (you could still have an interface to specified the methods commons to all), but should be an abstract class. What that means is, you cannot instatiate an instance of this class, only one of his children.

public abstract class BZMenuTable {

    protected Table menuTable;
    protected final Skin skin;
    private final Stage hudStage;
    private boolean isOpen;

    public BZMenuTable(Skin skin, Stage hudStage) {
        this.skin = skin;
        this.hudStage = hudStage;
        this.isOpen = false;
    }

    public boolean isOpen() {
        return this.isOpen;
    }

    public void open() {
        this.isOpen = true;
        this.menuTable = new Table(this.skin);
        this.hudStage.addActor(this.menuTable);
    }

    public abstract void build();

    public void close() {
        this.isOpen = false;
        this.menuTable.remove();
    }

}


It's now clear that the build() method should be implemented in all children.

Code Snippets

public void build() {
    //overridden by subclasses
}
public abstract class BZMenuTable {

    protected Table menuTable;
    protected final Skin skin;
    private final Stage hudStage;
    private boolean isOpen;

    public BZMenuTable(Skin skin, Stage hudStage) {
        this.skin = skin;
        this.hudStage = hudStage;
        this.isOpen = false;
    }

    public boolean isOpen() {
        return this.isOpen;
    }

    public void open() {
        this.isOpen = true;
        this.menuTable = new Table(this.skin);
        this.hudStage.addActor(this.menuTable);
    }

    public abstract void build();

    public void close() {
        this.isOpen = false;
        this.menuTable.remove();
    }

}

Context

StackExchange Code Review Q#83623, answer score: 4

Revisions (0)

No revisions yet.