How do you approach an object-oriented design question in an interview when you have to think out loud?
You talk through the design in four steps. You find the nouns, you find the rules, you decide where the boundaries go, and then you say how each object talks to the others.
public class Main {
public static void main(String[] args) {
OrderRepository repo = new InMemoryOrderRepository();
OrderService service = new OrderService(repo, new FlatFee(5.0));
service.place("Ada", 3);
System.out.println(repo.count());
}
}
And here is what the answer looks like when you talk through it.
the step what you say
---------------------------- --------------------------------------------
1. the nouns an order, a repository, a fee
2. the rules an order has lines and a total, a
repository stores and finds orders
3. the boundaries the repository is behind an interface
so it can be faked in the test
4. the messages the service asks the repository to
save and the fee for its price
And the first step is the one that people rush, and it is worth slowing down for. You say the nouns out loud and you check each one against the sentence test. So "an order has lines and a total, so it has rules and it wants to be a class" and "a fee is just a number and a name, so it is a record".
And the third step is where the design quality shows, and it is the one that the interviewer is listening for. So you say why the repository is behind an interface. "Because the test needs to run without a database and because the storage might change later."
public interface OrderRepository {
void save(Order order);
int count();
}
public class InMemoryOrderRepository implements OrderRepository {
private final java.util.List<Order> orders = new java.util.ArrayList<>();
@Override
public void save(Order order) {
orders.add(order);
}
@Override
public int count() {
return orders.size();
}
}
And there are three things that are worth knowing about for the interview itself. The first one is that you should think out loud even when you are unsure. The interviewer cannot help you if you go quiet, and they can tell that you are thinking from the words you use.
The second one is that you should ask questions. So "how many orders are we talking about" and "does the order need to know the customer or just the customer id". Those questions show how you design and they often change the answer.
The third one is that you should name the trade-offs as you go. So "I am making the repository an interface, which costs one more file, and the reason is that the test needs it". That sentence is worth a lot more than the code itself.
what to say why it helps
---------------------------- --------------------------------------------
"I am going to start with the it shows that you design from the
nouns and the rules" behaviour
"I will put the repository it shows that you think about
behind an interface so the testing and the change
test can fake it"
"the trade-off is one more it shows that you are making a
file" choice and not just following
a rule
So the rule to remember is this. Nouns, rules, boundaries, and then the messages between the objects.
The last question in this topic is about the questions that come up most often.