java – 访谈问题:设计模式来控制冰箱的行为

最近在我的一次采访中,我被问到一个有趣的问题.

You have a fridge that has been put by your organization 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.

我可以提出以下解决方案,但我对此不满意.

>为冰箱对象定义两个打开和关闭状态.这些只是冰箱的对象.
> open()和close()将通过创建这些对象来控制行为.当然,他们将是最终的.
>如果冰箱未处于有效状态,每个操作都将检查冰箱的状态,它会抛出UnSupportedOperation异常.

这是正确的解决方案还是可以有更好的方法?

最佳答案 我会计算门的速度和加速度.然后根据最小和最大阈值进行检查.当它几乎关闭并且有人将其关闭时,门必须具有最低速度并且不应超过最大速度等…

点赞