I had an interview yesterday and had asked an OOD question:
Race-Car Store System:
The system stores information about cars available for players.
- Two types of gear changing strategies: manual/automatic.
- Two types of fuel: gasoline/diesel.
Design a system that can produce cars requested by players (If the player wants a car with manual gear changing and burn diesel, your system should provide one instance of the car meets the requirements). The system should have good scalability and maintainability.
My thoughts and solution:
My thought is the requirement contains two attributes: gear and fuel. I plan to make an abstract class contains the attributes and corresponding behaviors. Considering scalability would have an interface Movable which contains behaviors what a car can do.
If any new attribute is added in the future, either create a new abstract class contains the new attribute or add the attribute into the existing abstract class, if there's new behaviors are required, I would either create new interface or add the behavior into existing interface.
Here's what I have done: An interface contains general behaviors, currently has showSpecs() only.
public interface Movable { public String showSpecs(); } An abstract class contains attributes fuel and gear
public abstract class Car implements Movable { String gear; String fuel; abstract void setFuel(String fuel); abstract String getFuel(); abstract void setGear(String gear); abstract String getGear(); } Now the race car class implementaion:
public class RaceCar extends Car { public RaceCar(String fuel, String gear) { this.fuel = fuel; this.gear = gear; } public void setFuel(String fuel) { this.fuel = fuel; } public String getFuel() { return this.fuel; } public void setGear(String gear) { this.gear = gear; } public String getGear() { return this.gear; } public String showSpecs() { StringBuilder sb = new StringBuilder(); sb.append("Gear:").append(this.gear); sb.append("Fuel:").append(this.fuel); return sb.toString(); } } Below is the main class I have:
public class Main { public static void main(String[] args) { System.out.println("get started..."); Car car = new RaceCar("diseal", "automatic"); System.out.println(car.showSpecs()); } } The interviewer replied that the solution I provided is not scalable and hard to maintain but didn't provide details so I am still confused about what mistakes I made and how to improve it.
Can anyone help share your thoughts and point out what am I supposed to improve?
Thanks!
MovableorCar: just define aRaceCarclass; give it two fields; give it a constructor. Define enums for the fuel and car fields' values. But scalability - I struggle to understand what is actually being asked for there: you invoke the constructor each time you want an instance; just invoking the constructor seems necessary and sufficient.Strings for the attributes. Better use enums (or something else, which depends on how those instances are actually used). And I don't like the requirements much because they don't tell much about how those instances are used. This makes it difficult to design something that is not just generic. Maybe they expected the factory pattern or strategy pattern. Who knows ..