0

Say I have like 50 different classes that extends "CustomEnchantment", is there any way to have a static getter in the abstract class that gets me one of the values of a specific class. Say I want max level of "Rage" then i'd do Rage.getMaxLevel() or something, in a static way. Problem is that getting an instance would mean i'd have to make 50+ instances 1 for each enchant. I want to just have a fix way to get a specific enchant's max level. I have tried to just include a "getMaxLevel()" and it returns it fine, but with that, i would need an instance of every single class that extends the abstract class, i'd rather have a static way of getting 1 specific class value. Some examples:

I have 3 classes extending Person. Person includes the variables age and name. In the class Josh, his name is set to "Josh" and age 17. In the class Jeff, his name is "Jeff" and age 20. In the class Jennica, name is "Jennica" and her age is 19. What I need is a method to return the age from a specific one of these classes at any time with only 1 method. So for example (This wont work) getAge(Jennica) gets Jennica's age, getAge(Josh) returns 17 for his age etc.. I need this to be used in a static or in a way where I can access it easily.

Daniel Holler
  • 235
  • 1
  • 5
  • 14

1 Answers1

0

By a static variable, you could realize that. You just have to take care that your static member is always updated by the subclasses max-level. If your code grows, that will make it unmaintainable.

A better approach could be to make an Manager-class like "CustomEchantmentManager" or something else. It could store a list of all your instances. Also, it could store an attribute like CustomEchantment enchantmentWithHighestLevel, where you store the instance with the highest level. Let your manager observe the enchantments and after a new level of one enchantment, check if it's level is higher than the actual stored enchantment's level and if yes, overwrite it's reference in the attribute. This will not change all your instances because it just references to your instance with the highest level.

Example for the manager:

public class CustomEnchantmentManager implements Observer {

    private ArrayList<CustomEnchantment> enchantments;
    private CustomEnchantment enchantmentWithHighestLevel = null;

    public CustomEnchantmentManager() {

        enchantments = new ArrayList<CustomEnchantment>();

    }


     @Override
     public void update(Observable obs, Object o) {
          if(this.enchantmentWithHighestLevel.getLevel() > ((CustomEnchantment)o).getLevel) {
               //do Nothing
           else {
               this.enchantmentWithHighestLevel = (CustomEnchantment)o;
     }
}

Example for an enchantment:

public class CustomEnchantment() extends Observable {

    public int level = 0;

    public CustomEnchantment() {


    }


    public void incrementLevel() {
        this.level++;
        setChanged(); //method from Observable
        notifyObservers(this); //send Observers instance of this object
    }
}
Supahupe
  • 515
  • 2
  • 11
  • So I should have a manager that would be creating and storing every instance is what you are saying, then have a static int where you pass in the enchant you want max level for etc.. and go from there? – Daniel Holler May 16 '16 at 13:27
  • This looks useful if I would have to increment the levels, but I am actually not looking into upping the levels, I have a level set in stone for every enchant, I just need a method where I can choose any enchant, and it would return the maximum level of the enchant. So if i'd need maximum level for Rage, i'd do (not sure about parameters) CustomEnchantmentManager.getMaxLevel(Rage.class) or something similar that would actually work, and if I'd use the same method but give it Swipe.class as paremeters it should return max level for enchant Swipe. – Daniel Holler May 16 '16 at 13:44