Java Design Question
Author |
Message |
Flikerator
|
Posted: Wed Aug 15, 2007 7:07 pm Post subject: Java Design Question |
|
|
Alright so I'm working through the thought process of designing Java (I'm not actually making anything, just testing and tinkering right now).
I have the main file (Stitches.java) with a units class (Unit.java) and an Infantry sublcass of Unit (seperate file, Infantry.java). In infantry is a nested class called "Horse" because someone infantry can have horses.
If I created crossbowman they wouldn't be able to use horses. Thats the reason I didn't nest horses in the Unit superclass. So each subclass would nest horses optionally. Would I be able to create a separate file called Horses.java and import it as a nested class?
There is also the instance where a unit would require a horse. In that case the subclass of Unit would be horses with a subclass of Lancers (Which you "can't" do explicitly in Java). My thought would be to just make horses forced with classes that require horses, but if there is another method I'm not aware of I'm all ears.
Mind the spacing. Tabs and [space] change the look from what I have in my IDE.
code: |
Unit
-> Infantry (Horses)
-> Crossbowman
-> Archers (Horses)
-> Horses
-> Lancers
or Lancers (Horses, Forced) |
Basically I'm wondering if I'm doing this "correctly" or if there are other ways (Better or worse, the more the better) to go about doing the same thing. This is for design ideas not an actual program using Infantry and Horses. Here are the programs I have;
Java: |
//Stitches.java
public class Stitches {
public static void newUnit (String fName, String lName) {
Unit tSoldier;
if (lName == "") {
tSoldier = new Unit (fName);
} else {
tSoldier = new Unit (fName, lName);
}
Unit.storeID(tSoldier);
}
public static void main(String[] args) {
newUnit ("Vasili","Zietseph");
newUnit ("Arnold","Smithsten");
Unit.displayUnits();
Infantry nSoldier = new Infantry ("Fred", "SmellyPants");
nSoldier.displayName();
}
} |
Java: |
//Unit.java
class Unit {
protected static Unit[] pointers = new Unit[99];
protected static int curID=0;
protected int id;
protected String fName, lName;
public Unit (String fName) {
id=curID++;
this.fName=fName;
this.lName="";
}
public Unit (String fName, String lName) {
id=curID++;
this.fName=fName;
this.lName=lName;
}
public String disp_fName() {return fName;}
public String disp_lName() {return lName;}
public static void storeID (Unit nId){
pointers [curID-1]=nId;
}
public static void displayUnits() {
System.out.println ("Number of units: " + curID);
for (int i=0; i<curID; i++) {
System.out.println (pointers[i].disp_fName() + " " + pointers[i].disp_lName()); //
}
}
} |
Java: |
//Infantry.java
class Infantry extends Unit {
class Horse {
boolean owned,currentlyMounted;
String name;
}
public Infantry (String fName, String lName) {
super (fName, lName);
}
public void displayName() {
System.out.println(fName+" "+lName);
}
} |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Aziz
![](http://compsci.ca/v3/uploads/user_avatars/17740604804829f8242e90c.png)
|
Posted: Wed Aug 15, 2007 10:13 pm Post subject: RE:Java Design Question |
|
|
Have a separate class, Horse. Each Unit can have zero or one horse(s).
Inheritance defines an "is-a" relationship. Infantry "is a" Unit. Lancer is NOT a Horse. What's the relationship between Horses and Unit? Well, some units can have horses, some cannot. So we could have a subclass of unit, that is MountedUnit. Our units that have horses would extend that class. MountedUnit would include a member of type Horse.
Just remember the Object Orientated principles of inheritance. |
|
|
|
|
![](images/spacer.gif) |
|
|