Interview Question: Design pattern to control behaviour of Fridge
Recently In one of my interview I was asked an Interesting question.
You have a fridge that has been put by your organiza开发者_开发技巧tion for use of employees. But Its observed that employees are not properly opening or closing door of that Fridge. E.g. Some kicks it to close, some might push it more that what it's needed to close it. So Now you have decided to control these two actions. Create a design for this problem.
I could come up with following solution but I am not satisfied with it.
- Define two states Open and Close for fridge object. These will be objects of Fridge only.
- open() and close() will control the behavior by creating these objects. Off-course they will be final.
- Every operation will check for state of fridge if its not in valid state it will throw an UnSupportedOperation exception.
Is it the right solution or there can be a better way?
I would calculate the speed and acceleration of the door. Then check it against min and max threshold values. When it's almost closed and somebody closes it, the door has to have a minimum speed and it shouldn't go above max speed, etc...
I would not think about Open and Close states since you only need to care about how the fridge is being closed.
So, whenever the door moves in the closing direction, the program would start. (Then what needs to be done has already been covered: speed control, minSpeed, etc)
For the configurable state problem, once you have a (secure) way to discern clients and employees, it's just a matter of having a second program switch the "fridgeSaver" on and off when needed.
plan B: Turn off the program and tell your employees that today they must not use the fridge since it is needed for the clients. They should understand. (to me, this is better them making them use some kind of ID whenever they open use the fridge... not considering computer vision in this)
I like your answer, but this question has many answers and I don't think there is one definitely correct answer. I'm sure they were just checking your problem solving and design skills.
To expand on yi's ideas, having an open and closed state, seeing how long a fridge is open before being closed, and having thresholds there as well. You could then tie in monitoring or other notification scheme when any threshold is exceeded.
Its just creating conversations and ideas.
I think what the interviewer was trying to explore is the knowledge on "abstraction" and "data hiding".
As the interviewer asked "some people kick to close" "some people push more than needed" etc, what he was looking for is to abstract one function called "close".
Fridge fridge = new Fridge();
fridge.close();
The answer would be - I would abstract the function called close() which will calculate how much force needed to close the fridge door and closes it. Once we do this, even if you kick or push more , the close method calculate what force needed to close and close it.
The implementation is hidden. It could implement a simple close button on the fridge which will use the power from a motor and close it. OR whatever force the user applies, it will just take the force needed for closing and closes the fridge nicely without making sounds :)
Encapsulation: I'm not sure this technically counts as a pattern (I haven't brushed up on them in a while), but it's a good rule to live by. Basically encapsulation means that you should only expose what is needed to use an object or control, and don't expose things that could end up creating unneeded dependencies. Typically, the fewer dependencies you have, the better.
精彩评论