how to organise my abstraction?
Posted
by
DaveM
on Stack Overflow
See other posts from Stack Overflow
or by DaveM
Published on 2012-09-14T15:35:45Z
Indexed on
2012/09/14
15:37 UTC
Read the original article
Hit count: 509
java
|abstraction
I have a problem that I can't decide how to best handle, so I'm asking for advice. Please bear with me if my question isn't clear, it is possiblybecause I'm not sure exacly how to solve!
I have a set of function that I have in a class. These function are a set of lowest commonality.
To be able to run this I need to generate certain info. But this info can arrive with my class from one of 2 routes.
I'll try to summarise my situation....
Lets say that I have a class as follows.
public class onHoliday(){
private Object modeOfTravel;
private Object location;
public onHoliday(Object vehicle, Location GPScoords)
{
private boolean haveFun(){//function to have fun, needs 4 people
}//end haveFun()
}
}
Lets imagine I can get to my holiday either by car or by bike.
my haveFun() function is dependant my type of vehicle.
But only loosely. I have another function that determines my vehicle type, and extracts the required values. for example if I send a car I may get 4 people in one go, but if I send I bike I need at least 2 to get the required 4
I have currently 2 options:
Overload my constructor, so as I can send either 2 bikes or a single car into it, then I can call one of 2 intermediate functions (to get the names of my 4 people for instance) before I haveFun() - this is what I am currently doing.
split the 2 constructors into 2 separate classes, and repeat my haveFun() in a third class, that becomes an object of my 2 other classes. my problem with this is that my intermediate functions are all but a few lines of code, and I don't want to have them in a separate file! (I allways put classes in separate files!)
Please note, my haveFun() isn't something that I'm going to need outside of these 2 classes, or even being onHoliday (ie. there is no chance of me doing some haveFun() over a weekend or of an evening!).
I have though about putting haveFun() into an interface, but it seems a bit worthless having an interface with only a single method! Even then I would have to have the method in both of the classes -one for bike and another for car!
I have thought about having my onHoliday class accepting any object type, but then I don't want someone accidentally sending in a boat to my onHoliday class (imagine I can't swim, so its not about to happen).
It may be important to note that my onHoliday class is package private, and final. It in fact is only accessed via other 'private methods' in other classes, and has only private methods itself.
Thanks in advance.
David
© Stack Overflow or respective owner