Hey again, so yeah i have been messing about trying to figure out the practicalities of the factory design pattern in java.
before i show you my code let me try to explain what i am trying to do. I am trying to create some functionality that will allow you to create any type of entity (lets say you wanted to build infantry as opposed to tanks) you wish on runtime and also allow me to extend on what types of entities i want to allow be created without having to mess around. So anyways here is what i have.
//this is actually the factory for creating entities.
package com.niroshido.gameObjects;
public class EntityFactory {
public Entity create(String name){
switch(name.toLowerCase()){
case "aircraft": Entity air = new Aircraft();
return air;
case "tank": Entity tank = new Tank();
return tank;
default: return null;
}
}
}
//yup this interface doesn't do anything, i had spent a while trying to figure out
//what if anything should be inside this interface, but i draw a blank.
package com.niroshido.gameObjects;
public interface Entity {
}
//this is a super class that most entites would inherit from, the exception being things that can't move/attack like //buildings
package com.niroshido.gameObjects;
public class Units implements Entity{
private int healthPoints;
private int attackDamage;
private int speed;
private int rotationSpeed;
public void move(){
}
public void drawUnit(){
}
public void attack(Entity e, Entity target){
}
public int getHealthPoints() {
return healthPoints;
}
public void setHealthPoints(int healthPoints) {
this.healthPoints = healthPoints;
}
public int getAttackDamage() {
return attackDamage;
}
public void setAttackDamage(int attackDamage) {
this.attackDamage = attackDamage;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getRotationSpeed() {
return rotationSpeed;
}
public void setRotationSpeed(int rotationSpeed) {
this.rotationSpeed = rotationSpeed;
}
}
//this is rather obvious, its a subclass, a specific type of unit.
package com.niroshido.gameObjects;
public class Tank extends Units{
public Tank(){
System.out.println("tank");
this.setHealthPoints(200);
System.out.println(this.getHealthPoints());
}
public void move(){
}
}
in my mind this is how i see my code as is
factory <- Creates- Entity <-implements- Units<-extends- Tank
now my main issue really comes down to, moving, drawing, attacking etc. In my factory class i decided to try and see if i could call a method from Tank, i can’t it wont allow me, i assume that this is actually logical and expected after all the factory design pattern is supposed to abstract the creation process and simply stop people from screwing around with the actual sub class of Units directly, am i right with this?. If this is the case what would the point of me having move or any of those methods in Units if i can’t access them as a result (at this point i clearly need to guidance), should i not have those methods there and simply create a different class for processing orders (attack, move, defend) that take a type of Entity as a parameter, I am sure there is a lot of people here who could understand what i am trying to achieve, possibly.
to describe this is a nut shell, i obsess about good code design, if i feel that if its not designed to the point it shines like a lighthouse at night i have failed miserably.