Object oriented design for a vehicle configuration kiosk from an interview
oop
Solution
There are a few options for that scenario.
First, the easy one:
Option #1 - Interface for Trucks/Vans
If only trucks and vans implement Options, then create an interface called IVehicleOptions and have trucks and vans implement it:
public interface VehicleOptions
{
Options { get; }
}
The drawback to this is that now you have to treat cars differently than trucks and vans.
Option #2 - Null Design Pattern
However, you can use the Null Design Pattern. Have Car implement IVehicleOptions, and just return null:
public class Car : IVehicleOptions
{
public VehicleOptions { get { return null; } }
}
Option #3 - Strategy Pattern
Create a base class, like Vehicle:
public abstract class Vehicle
{
public Options Options { get; protected set; }
}
And have each concrete class set it:
public class Car : Vehicle
{
public Car()
{
this.Options = NullOptions(); // This is the null design pattern used with this strategy pattern
}
}
public class Truck : Vehicle
{
public Truck
{
this.Options = SuperOptions();
}
}
public class Van: Vehicle
{
public Van
{
this.Options = ElegantOptions();
}
}
Now all vehicles can be treated the same way (as a Vehicle).
Problem
I had an interview question that asked how I would design a system to show cars/trucks/vans at a kiosk so customers could look at specs on the vehicles. (Warning: I have not done much OO programming lately so bad vocabulary may follow) I said I was start with a class, vehicle that had very basic properties like wheelbase, drive train, hp. Then I would subclass that into car, truck or van where I could have more specfic measurements like bed length for trucks or trunk capacity for cars. Then they asked, how would I add options. I said that options could probably exist on any vehicle so I would say that a vehicle could have a list of options. Finally they asked, what if there was an option that was only available on trucks and vans but not cars and I was a little stumped. Is there some way to make this nicely happen given the layout I described? Is there a better way to set up the class hierarchy to solve this? Or is this just a more complex problem that cant easily be solved without adding some extra logic?