简体   繁体   中英

how to organise my abstraction?

I have a set of functions 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 two routes.

I'll try to summarize 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
    }
}

Lets imagine I can get to my holiday either by car or by bike.

my haveFun() function is dependent 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 two options:

  1. Overload my constructor, so as I can send either two 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.

  2. split the two constructors into two separate classes, and repeat my haveFun() in a third class, that becomes an object of my two 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 always 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.

edit1:

I'm sure someone will suggest that I handle this in the class that send the data to the onHoliday() class. But this class (lets call it travel() ) has no reason to care about whay type of vehicle I am using, If I make it so it does care, I have simply moved the problem to another place further up the tree. and Now have to have all of my intermediate classes re-written to access either car or bike, for the moment they simply accept a vehicle object, that has a boolean switch to determine if it is a bike or a car.

edit2:

I've just had another though about having an abstract class for running my fun, then implementing it as an 'inner class' in either of my bike or car classes. Then I can access the members of my bike or class. does this make sense also?

Note: I should probably mention that I have to ensure in my onHoliday() clas that I hvae at least 4 people.

Maybe another way to think of it is...

I can get a multi page file, and extract the pages I need from a passed list of page numbers. Or I can get a list of multiple files.

In the first case I send a single file object, and the selection of page number in the second I send multiple files object (pre selecting the ones I am interested in).

In fact that is probably a better description

Your code sample has MANY compilation problems with it, but I think I know what you're asking. If you have an abstract class Vehicle , you can implement haveFun() in the concrete classes (ie Bike , Car . Your Holiday class constructor can take a Vehicle parameter. No matter what type of vehicle it is, the implementation is outside of Holiday - rather as part of the Vehicle.

You did say it was a loose coupling though between fun and vehicle. If it doesn't make sense to implement haveFun in the Vehicle, then just have a method in Holiday that determines it - the negative to this method though is that you'll have to check for each type of Vehicle you have (ie if vehicle instanceof Bike then... else if.... )

If you go with the latter, you can'd to anything in the contructor. You would want to have a method that say add's persons and Vehicles... addPerson(Vehicle) . Then after all have been added run isFun().

I might propose a third approach: create a Vehicle class which handles the people-moving.

public abstract class Vehicle {

    public void travel(Location location) {}

    public abstract int getCapacity();

    //etc.
}

public class Bicycle extends Vehicle {

    @Override
    public int getCapacity() { return 2; }

}

public class Car extends Vehicle {

    @Override
    public int getCapacity() { return 4; }

}

final class onHoliday {

    private Vehicle vehicle;

    public onHoliday(Vehicle vehicle) {
        this.vehicle = vehicle;
        haveFun();
    }
}

Then, if you have any things that a Car and a Bike would do differently (but that they would both do), just put them abstract in the Vehicle class and override.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM