I am currently working on a train simulation project for uni.
This is my class hierarchy:
RollingStock
Coach
FreightCoach
PassengerCoach
SpecialCoach
Engine
DieselEngine
ElectricEngine
SteamEngine
Trainset
My questions:
I've created a class "SharedIdSpace" to implement this feature. But I am not quite sure how to solve this nicely (TreeMap, ..., ?).
Now, my main problem is I have to implement the following feature:
"Rolling stock can be composed into a train. The following restrictions must be observed when composing:
How can I implement this? I'm afraid I have no useful idea.
Im not sure about what are you asking, but I will try to give you my aproximation:
You need to create a composite object where every object has an unique ID and need to pass some validations.
I would implement an "StoregeManager" a "ComposerManager" and add an abstract validator method in "RollingStock" that validate if the vagon can be added.
The flow would be something like this:
DISCLAIMER This is written in Java, but with notepad++, please dont check the sintaxis.
RollingStock freightCoach = StoregeManager.getFreightCoach();
RollingStock specialCoach = StoregeManager.getSpecialCoach();
RollingStock dieselEngine = StoregeManager.getDieselEngine();
// Check if they are null or throw an exception if has no more elements. Maybe from BBDD or from where you want
Composer.compone()
.add(dieselEngine)
.add(freightCoach)
.add(specialCoach)
.build()
And inside the componer, something like this:
public class Composer {
private StoregeManager storeManager; //Injected or initialized, as you want.
private static Train train;
public Composer build(){
train = new Train;
return this;
}
public Composer add(RollingStock rs) {
if(rs.isValid(train))
train.add(rs);
return this;
}
public RollingStock[] build() {
storageManager.ckeckTrain(train);
return train;
}
}
You can put the storage inside the Composer and pass as argument the classname of the vagon if you need another aproximation to your problem.
I hope this helps you.