I have a question as to the what is the better approach to solving my problem.
I am writing a simple car object and am using composition as the means of constructing the components of this object. Let's just say I have a car object in which I instantiate objects of types Keys and Ignition. I then invoke the methods to insert the keys, turn on the ignition, turn off the ignition and then finally remove the keys. However, in the ignition class, I must check to see if the keys are inserted before I turnon the ignition. In order to do this, I must query the keys object directly. What is the best approach to do this. I thought of 2 ways to do this. The issue at hand is simply visibility. if I try to look at the keys object from the ignition object, the compiler says it can not resolve to the keys object. so my first thought was, instead of declaring the references to the keys and ignition objects in the main method of my application class, I could declare them as class variables. This did work for the scenario, but now I have 2 static classes objects and would like to know what the pros and cons are to taking this approach. Finally, my last idea was to just declare them as I had originally within the main method, but when I called the ignition method, I thought I could simply pass the keys object in which I would be able to the query the object directly. I feel this is probably the best way since I dont have to go out of my way to modify visibility to an object. I simply manage the situation using messaging. Can any one tell me if this is the best approach or if there is even yet another way that would be considered better.
thank you
publicclass ACar{
AKeys keys = new AKeys();
AIgnition ignition = new AIgnition();
publicstaticvoid main(String[] args){
keys.insert();
ignition.turnOn();
ignition.turnOff();
keys.remove();
}
}
class AIgnition{
privateboolean started = false;
publicvoid turnOn(){
//this is where I want to query the keys state...
boolean keysInserted = ACar.keys.getKeyState();
if(keysInserted==true){
started = true;
System.out.println("Ignition started");
}
else
System.out.println("Insert the keys before attempting to start");
}
publicvoid turnOff(){
boolean keysInserted = ACar.keys.getKeyState();
if(keysInserted==true){
started = false;
System.out.println("Ignition Off");
}
else
System.out.println("Already off, Keys are not in the ignition");
}
publicboolean getIgnitionState(){
return started;
}
}
class AKeys{
privateboolean inserted = false;
publicvoid insert(){
if(inserted == true)
System.out.println("Keys are already inserted");
else{
inserted = true;
System.out.println("Keys inserted");
}
}
publicvoid remove(){
if(inserted == false)
System.out.println("Keys are already removed");
else{
inserted = false;
System.out.println("Keys removed");
}
}
publicboolean getKeyState(){
return inserted;
}
}
p.s. I formatted this but preview is making it look like it isnt, I am going to submit and hope that it works correctly... please understand if the code comes out bad.
IMHO, what you are doing is NOT composition. Here is a suggestion on how I would design the application. The design of the other classes will follow from it.
Put all of the rest of your code in separate files for the AKey class, the ACar class and the AIgnition class. The car is an object and should be defined in its own class. The ignition is an object in a car, so the car class will have an ignition as one of its instance variables. Since keys go with the car, I can see a key object also being an instance variable of the car or possibly of the ignition.