Interview Question: Design pattern for controlling refrigerator behavior

Recently, in one of my interviews I was asked an interesting question.

You have a refrigerator that your organization has supplied for use by employees. But he noticed that the staff did not open or close the door of this refrigerator. For instance. Some kick him to close, some might push him to what he needed to close. So, now you have decided to control these two actions. Create a design for this problem.

I could find the following solution, but I am not satisfied with this.

  • Define two Open and Close states for the refrigerator object. These will be only objects of the refrigerator.
  • open () and close () will control the behavior by creating these objects. Off course they will be final.
  • Each operation checks the status of the refrigerator; if it is not in the correct state, it throws an UnSupportedOperation exception.

Is this the right decision, or maybe the best way?

+4
source share
5 answers

I would calculate the speed and acceleration of the door. Then check it for the minimum and maximum threshold values. When it is almost closed and someone closes it, the door should have a minimum speed and should not exceed the maximum speed, etc.

+1
source

I would not think about the states of Open and Close, since you only need to take care of how the refrigerator closes.

So, whenever the door moves in the closing direction, the program starts. (Then you need to do what needs to be done: speed control, minSpeed, etc.)

For a configurable state problem, when you have a (secure) way to recognize clients and employees, it is simply a matter of having the second program turn the refrigerator on and off if necessary.

Plan B: Turn off the program and tell your employees that they should not use the refrigerator today, as this is necessary for customers. They must understand. (for me, this is better than forcing them to use some kind of identifier when they open the use of the refrigerator ... without considering computer vision in this)

+1
source

I like your answer, but this question has many answers, and I do not think that there is a clearly correct answer. I'm sure they just tested your problems and design skills.

0
source

To expand yi's ideas, having an open and closed state, seeing how long the refrigerator opens before closing and also has thresholds. You can then link monitoring or another notification scheme when any threshold is exceeded.

Just creating conversations and ideas.

0
source

I think the interviewer was trying to investigate this knowledge of “abstraction” and “data hiding”.

As the interviewer said, “some people begin to close,” “some people press more than necessary,” etc., what he was looking for is an abstraction of one function called “close.”

Fridge fridge = new Fridge(); fridge.close(); 

The answer will be: I would single out a function called close (), which will calculate how much force you need to close the refrigerator door and close. When we do this, even if you press or press more, the close method will calculate what force needs to be closed and closed.

The implementation is hidden. He can implement a simple close button on the refrigerator, which will use the power from the engine and close it. OR no matter what the user applies, he will simply take the force necessary to close and close the refrigerator beautifully, without sounds :)

Encapsulation:. I'm not sure if this is technically considered a template (I haven't clicked on them for a while), but this is a good rule for life. Basically, encapsulation means that you should only expose what is needed to use an object or control, and not expose things that can lead to the creation of unnecessary dependencies. Generally, the less dependencies you have, the better.

0
source

All Articles