n*******e 发帖数: 37 | 1 对OOP的面试问题十分头痛, 自己想的solution也不知好不好, 希望版上高手能指点一
下.
之前面试遇到以下的问题, 想与大家讨论讨论~
1. Design a smart house controller which can control electronic devices in
house
Suppose in a house, there are many different types of electrical devices,
such as telephone, television, washing machine, microwave oven... All these
devices will be controlled by a smart house controller, ex. makes the
television to increase volume. More advanced, the smart house controller can
schedule task for each device for a specific time, ex, schedule a washing
machine to automatically start washing clothes on 2:00pm.
2. Design an airport with Air Traffic Control system
Any idea is appreciated! | H******7 发帖数: 1728 | 2 好题目啊
第一题 观察者模式要用到
these
★ 发自iPhone App: ChineseWeb 8.7
【在 n*******e 的大作中提到】 : 对OOP的面试问题十分头痛, 自己想的solution也不知好不好, 希望版上高手能指点一 : 下. : 之前面试遇到以下的问题, 想与大家讨论讨论~ : 1. Design a smart house controller which can control electronic devices in : house : Suppose in a house, there are many different types of electrical devices, : such as telephone, television, washing machine, microwave oven... All these : devices will be controlled by a smart house controller, ex. makes the : television to increase volume. More advanced, the smart house controller can : schedule task for each device for a specific time, ex, schedule a washing
| t*****3 发帖数: 112 | 3 面试给你多少时间?正常情况下这应该是OOD吧,OOP的话一天都写不完。抛个砖:
core objects:controller, house, room (living, bed, kitchen, bath, storage),
devices (tv, phone, door, microwave, etc.)
relationships: house {a controller, multi-rooms}; room {multi-devices};
controller {devices}; device{actions}
methods:
house: startController(), stopController(), getRooms(), getDevices()
room: getDevices(), doDevices(), doDevice()
controller: start(), stop(), scheduleDevices(), scheduleDevice(),
doDevices(), doDevice()
device: start(), stop(), schedule(), ...
house是入口,然后可以找room,找device,提供id给controller去操作
these
can
【在 n*******e 的大作中提到】 : 对OOP的面试问题十分头痛, 自己想的solution也不知好不好, 希望版上高手能指点一 : 下. : 之前面试遇到以下的问题, 想与大家讨论讨论~ : 1. Design a smart house controller which can control electronic devices in : house : Suppose in a house, there are many different types of electrical devices, : such as telephone, television, washing machine, microwave oven... All these : devices will be controlled by a smart house controller, ex. makes the : television to increase volume. More advanced, the smart house controller can : schedule task for each device for a specific time, ex, schedule a washing
| n*******e 发帖数: 37 | 4 感谢回复!! 是的, 应该是OOD才对.
我那时的回答中, 只有house controller和devices class. 加上house和room class就
完整很多.
另外, 在答题时遇到以下两大难题:
1. controller提供的doDevices() method, 可以对各种device subclass做各种task.
有一个难题是, 不同subclass的device有非常不同的task. 例如: tv有set volume,
washing machie有wash, telephone有make call ... 这些task都需要subclass有自己
的specific method.
这样的话, 要如何用controller提供的doDevices()对所有device处理各种task呢?
2. scheduleDevices()应该要setup一个event handler, 当timer event expired时,
就trigger这个event handler? 但具体来说, 如何用C++实现呢? 感觉这种功能应该会
很常用, 但网路上查不到简单明了的C++实现方法.
OOD问题真是难啊, 不知道这些是不是非常基本的问题
),
【在 t*****3 的大作中提到】 : 面试给你多少时间?正常情况下这应该是OOD吧,OOP的话一天都写不完。抛个砖: : core objects:controller, house, room (living, bed, kitchen, bath, storage), : devices (tv, phone, door, microwave, etc.) : relationships: house {a controller, multi-rooms}; room {multi-devices}; : controller {devices}; device{actions} : methods: : house: startController(), stopController(), getRooms(), getDevices() : room: getDevices(), doDevices(), doDevice() : controller: start(), stop(), scheduleDevices(), scheduleDevice(), : doDevices(), doDevice()
| t*****3 发帖数: 112 | 5 第1个问题:
如果是个简单的controller
controller: doDevice(deviceID, commands)
这里doDevice不是一个固定的名称,而是针对不同设备定义的,比如doWashMachine()
,然后具体怎么调用设备命令就在里面具体实现。这样的好处是结构简单,很容易实现
,不利之处是随着设备增多和需求变化,controller会变得非常难以维护。
如果是要求比较复杂的controller:
controller: runTasks(), runTask()
Task: {multi-rooms, multi-devices} run(), pause(), stop(), restart()
Task类可以是个抽象类或者接口,供下层具体实现逻辑,得到device的实例然后调用相
应的方法。
第2个问题:
我用java,c++不是很懂。但是我觉得这里controller得用多线程的思路。
简单的做法是每个task开一个线程,然后自己去查询时间是否到了。好处是实现起来简
单,坏处是随着task增多,对系统资源的占用也比较大。
高级点做法:用个queue按时间顺序存放等待执行的task,一个timer线程定期(比如15
分钟)来看一下,执行相应的任务(task)。
Queue schedule_queue;
Thread schedule_timer;
这里把task放入schedule_queue会是另一个线程的工作,那么就需要提供thread safe
的处理,java里面有现成的同步机制synchronized、lock啥的。如果是c++可能就需要
用相应的api库来处理了吧。
工作中的做法是直接找现成的中间件,可以提供queue和timer,我们只需要定义好task
和event即可。
design问题感觉考察具体技术少一点,考察思路更多一些。否则直接问相关主题即可,
何必绕这么大的弯子。所以如果我们感觉到一个技术难点成为show stopper的时候,可
能是思考错了方向。
.
【在 n*******e 的大作中提到】 : 感谢回复!! 是的, 应该是OOD才对. : 我那时的回答中, 只有house controller和devices class. 加上house和room class就 : 完整很多. : 另外, 在答题时遇到以下两大难题: : 1. controller提供的doDevices() method, 可以对各种device subclass做各种task. : 有一个难题是, 不同subclass的device有非常不同的task. 例如: tv有set volume, : washing machie有wash, telephone有make call ... 这些task都需要subclass有自己 : 的specific method. : 这样的话, 要如何用controller提供的doDevices()对所有device处理各种task呢? : 2. scheduleDevices()应该要setup一个event handler, 当timer event expired时,
| m*******g 发帖数: 410 | | n*******e 发帖数: 37 | 7 谢谢了!!!
大概有方向了
【在 t*****3 的大作中提到】 : 第1个问题: : 如果是个简单的controller : controller: doDevice(deviceID, commands) : 这里doDevice不是一个固定的名称,而是针对不同设备定义的,比如doWashMachine() : ,然后具体怎么调用设备命令就在里面具体实现。这样的好处是结构简单,很容易实现 : ,不利之处是随着设备增多和需求变化,controller会变得非常难以维护。 : 如果是要求比较复杂的controller: : controller: runTasks(), runTask() : Task: {multi-rooms, multi-devices} run(), pause(), stop(), restart() : Task类可以是个抽象类或者接口,供下层具体实现逻辑,得到device的实例然后调用相
| n*******e 发帖数: 37 | 8 真的很难啊 尤其是没什么OOP实战经验
【在 m*******g 的大作中提到】 : 难为楼主,马龙相关不好搞啊。
| m*****k 发帖数: 731 | 9 sounds like visitor pattern
【在 t*****3 的大作中提到】 : 第1个问题: : 如果是个简单的controller : controller: doDevice(deviceID, commands) : 这里doDevice不是一个固定的名称,而是针对不同设备定义的,比如doWashMachine() : ,然后具体怎么调用设备命令就在里面具体实现。这样的好处是结构简单,很容易实现 : ,不利之处是随着设备增多和需求变化,controller会变得非常难以维护。 : 如果是要求比较复杂的controller: : controller: runTasks(), runTask() : Task: {multi-rooms, multi-devices} run(), pause(), stop(), restart() : Task类可以是个抽象类或者接口,供下层具体实现逻辑,得到device的实例然后调用相
| s********l 发帖数: 998 | 10 第一题 是observer吗??
难道不应该是command pattern?!
【在 H******7 的大作中提到】 : 好题目啊 : 第一题 观察者模式要用到 : : these : ★ 发自iPhone App: ChineseWeb 8.7
|
|