There is a question which was recently asked to me in an interview.
Problem: There is a class meant to profile the execution time of the code. The class
First there is the fact that implementing an own Java profiler is a waste of time, since good ones are available (maybe that was the intention behind the question).
If you want to enforce the correct method order at compile time, you have to return something with each method in the chain:
start()
has to return a WatchStopper
with the stop method.WatchStopper.stop()
has to return a WatchResult
with the getResult()
method.External Construction of those helper classes as well as other ways of accessing their methods have to be prevented of course.