I wanted to know if I'm violating any solid principles with a quick console based demo I've coded up for demonstration purposes. My main concern is with my player class. I have quite a few methods in the class that do different types of things but they are all related to the player. Is this in any way violating the SRP principle? Also, I wanted to know if my abstractions are somewhat good, is this is my coding style when developing more serious apps.
I have less than a year experience and I'm currently learning all the solid principles and to be honest, I've now became exceedingly paranoid about whether I write good code or not.
Thanks for the help.
public class Entity { //Base template for all game entities...
private String name;
public Entity(String name)
{
this.name = name;
displayEntityName();
}
public String getName()
{
return name;
}
public void setName(String newName)
{
name = newName;
}
private void displayEntityName()
{
System.out.println(name + " entity created...");
}
public class Player extends Entity {
private String[] inventory; //Player will have an inventory.
private int itemsInInventory;
private static final Random RAND = new Random();
public Player(String name) {
super(name);
inventory = new String[25]; //Default inventory size.
}
public void attack(Enemy enemy) //Player can attack all enemy types.
{
System.out.println(super.getName() + " attacked the enemy...");
}
public void findLegendaryWeapon()
{
inventory[0] = WeaponConstants.LEGENDARY_WEAPONS
[RAND.nextInt(WeaponConstants.LEGENDARY_WEAPONS.length)];
itemsInInventory++;
}
public void equiptWeapon()
{
System.out.println(super.getName() + " equipted a weapon...");
}
public void searchChest()
{
System.out.println(super.getName() + " searched the chest and found nothing...");
}
}
public enum WeaponConstants{ //Legendary weapon constants.
BOW_OF_THE_MAJOR_ARCHER(), BLOOD_LETTER,
DAWNFANG, FROZEN_TOUCH;
public static final String[] LEGENDARY_WEAPONS = {DAWNFANG.name(),
BLOOD_LETTER.name(), FROZEN_TOUCH.name(), BOW_OF_THE_MAJOR_ARCHER.name()};
}
public interface Enemy { //Enemy interface...
void attack(Player player);
}
public class Ghoul extends Entity implements Enemy { //An example of a standard enemy class.
public Ghoul(String name) {
super(name);
}
@Override
public void attack(Player player) {
System.out.println(super.getName() + " attacked " + player.getName());
}
}
Aucun commentaire:
Enregistrer un commentaire