samedi 28 mars 2015

Java - making a class more generic

Here's some code I've inherited for a game. The sample code creates Armor.


At the moment to make some new Armor, you need to write a new class. E.g.



// Armor.java
public class Armor extends Item {
public int tier;

public Armor( int tier ) {
this.tier = tier;
}
}


and



// ClothArmor.java
public class ClothArmor extends Armor {
{
name = "Cloth armor";
}

public ClothArmor() {
super( 1 );
}

@Override
public String desc() {
return "Some Cloth Armor.";
}
}


How would you structure the code to make it more generic? It would seem obvious to just read from a text-based config file but I can see this running into problems when you wanted to create an Armor with special abilities for example.


Are there any resources or design patterns I can use to figure out how to proceed?


Aucun commentaire:

Enregistrer un commentaire