Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class Design

Status
Not open for further replies.

osubb

Programmer
Jan 21, 2004
11
US
I'm pretty new to OOAD. I'm trying to come up with a good design on my new project. I'll describe the problem and hope I get some ideas from some people.

Let's say you we have a class "Vehicle". This class will be used in many different industries (at least 10-15). The Vehicle class has all the "things" (horn, flasher, lights, wheels, etc.) that could be used in all industries. Depending on the industry, each "thing" will be either required, optional, or can not exist.

I did not want to create just the Vehicle class and put all logic for each diffenent industry into it. I thought that I could use inheritance and then build subclasses. The problem is, a lot of industries use the same member/things, but whether they are optional/required might be different. It seemed I'd be making a lot of subclasses which could be very similar. There really is no base set of "things" that all use - there all pretty different.

I was thinking of using interfaces to differentiate the type of Vehicle based on industry (has methods only for the required/optional things). I also thought of using a Static Utitlity Class for checking if all required data is present after the class/object is populated (error checking).

Is this a sound approach? I really didn't care too much for the interfaces either, there would be alot.

Any suggestions on a better design.

Larry
 
Code:
public interface vehicle
{
    // mandatory:
    public void go_forward ();
    public void push_break ();
    public void turn (Direction foo);

    // optional:
    public void fill_tank (int gallons);
}

public class sailing_ship implements vehicle
{
    public void fill_tank (int gallons)
    {
         throw new NotSupportedException ("sailing ship without tank";
    }
}

This isn't the best design, but perhaps a possible trick.
Of course, if you now have a collection of vehicles, and like to fill every tank with 5 gallons...
so if we modify the vehicle:

Code:
public interface vehicle
{
    //...
    
    // for every optional method:
    public boolean may_fill_tank ();
}

...
{
    for (v in vehicles)
    {
         if (v.may_fill_tank)
             v.fill_tank (5);
         // avoids NotSupportedException
    }
}

Now you may even define the method 'fill_tank' in a Superclass, and redirect every call to 'fill_tank' to this superclass, and test for 'may_fill_tank' in the first line of this method.

Is this an approach?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top