The Artima Developer Community
Sponsored Link

Java Answers Forum
simple opinion requested on best OOP solution

1 reply on 1 page. Most recent reply: Apr 21, 2004 9:01 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

simple opinion requested on best OOP solution Posted: Apr 21, 2004 7:41 AM
Reply to this message Reply
Advertisement
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


public class ACar{
 
	AKeys keys = new AKeys();
	AIgnition ignition = new AIgnition();
 
	public static void main(String[] args){
 
		
		keys.insert();
		ignition.turnOn();
		ignition.turnOff();		
		keys.remove();
 
	}
 
}
 
class AIgnition{
 
	private boolean started = false;
 
	public void 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");
	}
	public void 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");
}
 
	public boolean getIgnitionState(){
 
		return started;
	}
 
}
 
class AKeys{
 
private boolean inserted = false;
 
public void insert(){
	if(inserted == true)
		System.out.println("Keys are already inserted");
	else{
		inserted = true;
		System.out.println("Keys inserted");
	}
}
public void remove(){
	if(inserted == false)
		System.out.println("Keys are already removed");
	else{
		inserted = false;	
		System.out.println("Keys removed");
	}
}
 
public boolean 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.

thanks again


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: simple opinion requested on best OOP solution Posted: Apr 21, 2004 9:01 PM
Reply to this message Reply
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.

public class RunCar{
     public static void main(String[] args){
       ACar theCar = new ACar();
       AKeys theKeys = theCar.getKeys();
       theCar.insertKeys(theKeys);
       theCar.turnOn();
       theCar.turnOff();
       theCar.removeKeys(theKeys);
     }
}


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.

I hope this helps.

twc

Flat View: This topic has 1 reply on 1 page
Topic: Make Data Accessible Throughout Application Previous Topic   Next Topic Topic: urgent - conversion problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use