Nestor Ledon
Nestor Ledon

Reputation: 1992

Java OOP: Building Object Trees / Object Families

Been a while since I used Java and was wondering if this was a decent or even correct way of setting this up.

FYI, userResults refers to a JDBI variable that isn't present in the code below.

Feel free to suggest a better method, thanks.

public class Stat
{
private int current;
private int max;

public int getCurrent()  {return current;}
public void setCurrent(int current)   {this.current = current;}

public int getMax()  {return max;}
public void setMax(int max)   {this.max = max;}
}


public class Character
{
Stat hp = new Stat();
Stat mp = new Stat();
}

Character thisCharacter = new Character();

// Set the value of current & max HP according to db data.
thisCharacter.hp.setCurrent((Integer) userResults.get("hpColumn1"));
thisCharacter.hp.setMax((Integer) userResults.get("hpColumn2"));

// Print test values
System.out.println (thisCharacter.hp.Current);
System.out.println (thisCharacter.hp.Max);

Upvotes: 0

Views: 175

Answers (3)

Brady
Brady

Reputation: 10357

I would consider creating the Stat objects seperately and passing them into Character, and making the character attributes private as follows:

public class Character
{
  private Stat hp;
  private Stat mp;

  public Stat getHp()         {return hp;}
  public void setHp(Stat h)   {this.hp = h;}

  public Stat getMp()         {return mp;}
  public void setMp(Stat m)   {this.mp = m;}
}

// Set the value of current & max HP according to db data.
Stat hp = new Stat();
hp.setCurrent((Integer) userResults.get("hpColumn1"));
hp.setMax((Integer) userResults.get("hpColumn2"));

Character thisCharacter = new Character();
thisCharacter.setHp(hp);
// do the same for mp

One additional simple step would be to create a Character constructor that would take an hp and an mp

Upvotes: 0

Spoike
Spoike

Reputation: 121792

Correct? Well, does it work? Then it probably is correct.

Wether or not it is a decent way to do it then the answer is "maybe". It is hard to tell from what context this code is in. But there are some things you could keep in mind though:

  • In which class (or object rather) are the Stat set in? Do you feel is it the responsibility of the class to do this and know what database values to get them from? If not, consider making some kind of a class that does this.
  • Making chained calls such as thisCharacter.hp.setCurrent(...) is a violation of principle of least knowledge. Sometimes you can't help it, but usually it leads to kludgy code. Consider having something that handles all the logic surrounding the stats. In your code you may need a HealthStatsHandler that have methods such as loadStats(), saveStats(), and mutator actions such as takeDamage(int dmg) and revive(int health).
  • If you have trouble figuring things out if it has the correct object design, then study up on the SOLID principles. They provide nice guidelines that any developer should follow if they want to have code that is extensible and "clean".

Upvotes: 1

adranale
adranale

Reputation: 2874

This is not really a tree. It is not possible two have more than one layer of children.

Usually you define an interface let's call it Node where both Stat and Character implements it and the two children of Character would have the type Node.

Upvotes: 0

Related Questions