I have four different classes. First being a base class for the second and third. The fourth being a container class that holds instances of second and third (stored as first).
My base class for PC
and Companion
public class Controllable
{
protected String name;
protected int x;
public Controllable (String name) {
this.name = name;
}
public void move () {
x++;
}
}
public class PC extends Controllable
{
private int someUniqueVariable;
public PC (String name, int someUniqueVariable) {
super (name);
this.someUniqueVariable = someUniqueVariable;
}
}
public class Companion extends Controllable
{
public Companion (String name) {
super (name);
}
}
And finally the wrapper/container class.
public class Party
{
private PC pc;
private List<Controllable> members;
public Party () {
PC = new PC ("PC", 1); // edit #1: should be pc, bad copy-paste
members = new ArrayList<Controllable>();
members.add (PC);
members.add (new Companion ("Companion1");
members.add (new Companion ("Companion2");
}
public moveAll () {
for (Controllable m : members) {
m.move();
}
}
public getPC () {
return pc;
}
}
Now if I want to move all Controllable
I will iterate over members
. If there are pc
specifics I will simply call the PC and use its move function. PC
will be more separated from Companion
later on.
I would mostly like feedback on the design of my code.
Edit #1:
The most critical area were I want feedback is this. Most things are shared between Companion
and PC
, however there are some unique features of PC
. In 99% I want to be able to iterate over all of the party members In more rare cases I want to only access the Companion
s and sometimes I only want to access the PC.
public Party () {
pc = new PC ("PC", 1);
members = new ArrayList<>();
members.add (PC);
members.add (new Companion ("Companion1");
members.add (new Companion ("Companion2");
}
Minor edit to show an error in the code.
Party
. – Emz Dec 2 '14 at 1:11