Amazon OO Design 经典题的思考# JobHunting - 待字闺中
l*n
1 楼
最近开始准备OO design questions, 这两天学习了一下design pattern, 用Amazon了
经典的Hotel Reservation OO
Design的问题练习了一下。希望能和版上各位高手交流一下。
(1) 问题1: 这个system应该就那些类
在version 1设计,我定义了如下类Room, SingleRoom, DoubleRoom, SuitRoom,
Hotel, Customer,
Reservation, Date。 因为感觉这个design,主要就是如何设计check different types
of rooms' availability和make
reservation。
(2) 问题2: 这些类的关系应该如何
其实就是"has-a" and "is-a" 的设计,也就是用inheritance或是composition
。根据直觉定了了如下的UML图
以上两步估计大部分人都能得道,不过如何才能打动interviewer呢?怎么才能提高这
个设计呢?
我想可以从两各方面入手:(1)program with interface (不过这个好像是java
programmer的rule) (2)多用
design pattern, 因为design pattern能decouple code,这样就能提高code的
resuability. 在什么情况下,如何使用
design pattern就是门学问了,需要经验阿!
不过对于这个问题,我想了些改进方法。
(3) 问题3:如何create object
用factory method pattern来建造不同的Room object, 虽然目前的design只有三个
Room-based classes,不过随着以
后系统复杂度的增加,可能回有n个Room-based classes。我猜想design的时候考虑日
后扩展的flexibility,应该也是
interviewer喜欢看到的把。
(4) 问题4: 如何hold多个Room-based objects
是用一个list来hold所有的SingleRoom, DoubleRoom, SuitRoom的object,还是用三个
list来hold各自的object? 这个
没有想好,目前就用了一个list, 因为估计总room的数目不会太大,对单个list进行
iterate的效率不会太差。
(5) 问题5: 如何有效的check availability.
假设有一个customer要查询每种room从某天到某天的availability, 那么需要对所有的
Room subclasses都定义一个查
询函数checkAvailabity()。这样的话不是很好的design。因为日后要加其他的查询:
如Room的朝向(如,有没有view
之类的:-)),那又再改interface, 然后对所有的classes添加函数。麻烦!
所以, 我考虑用strategy pattern可以日后extend各种查询。
(6) 问题6: 找到availability, 如何设计reservation
回想一下,make a reservation其实需要多个步骤的,需要填用户信息,需要填信用卡
(如果是pre-pay的话),需要
计算tax, 还可以用discount code的,最后得到total amount。这些步骤都是
sequential的,有一定的次序。所以我们
可以用builder pattern来generate所有这些步骤。
写个code(看下面link),欢迎各位排砖。
http://www.ideone.com/KsDCV
经典的Hotel Reservation OO
Design的问题练习了一下。希望能和版上各位高手交流一下。
(1) 问题1: 这个system应该就那些类
在version 1设计,我定义了如下类Room, SingleRoom, DoubleRoom, SuitRoom,
Hotel, Customer,
Reservation, Date。 因为感觉这个design,主要就是如何设计check different types
of rooms' availability和make
reservation。
(2) 问题2: 这些类的关系应该如何
其实就是"has-a" and "is-a" 的设计,也就是用inheritance或是composition
。根据直觉定了了如下的UML图
以上两步估计大部分人都能得道,不过如何才能打动interviewer呢?怎么才能提高这
个设计呢?
我想可以从两各方面入手:(1)program with interface (不过这个好像是java
programmer的rule) (2)多用
design pattern, 因为design pattern能decouple code,这样就能提高code的
resuability. 在什么情况下,如何使用
design pattern就是门学问了,需要经验阿!
不过对于这个问题,我想了些改进方法。
(3) 问题3:如何create object
用factory method pattern来建造不同的Room object, 虽然目前的design只有三个
Room-based classes,不过随着以
后系统复杂度的增加,可能回有n个Room-based classes。我猜想design的时候考虑日
后扩展的flexibility,应该也是
interviewer喜欢看到的把。
(4) 问题4: 如何hold多个Room-based objects
是用一个list来hold所有的SingleRoom, DoubleRoom, SuitRoom的object,还是用三个
list来hold各自的object? 这个
没有想好,目前就用了一个list, 因为估计总room的数目不会太大,对单个list进行
iterate的效率不会太差。
(5) 问题5: 如何有效的check availability.
假设有一个customer要查询每种room从某天到某天的availability, 那么需要对所有的
Room subclasses都定义一个查
询函数checkAvailabity()。这样的话不是很好的design。因为日后要加其他的查询:
如Room的朝向(如,有没有view
之类的:-)),那又再改interface, 然后对所有的classes添加函数。麻烦!
所以, 我考虑用strategy pattern可以日后extend各种查询。
(6) 问题6: 找到availability, 如何设计reservation
回想一下,make a reservation其实需要多个步骤的,需要填用户信息,需要填信用卡
(如果是pre-pay的话),需要
计算tax, 还可以用discount code的,最后得到total amount。这些步骤都是
sequential的,有一定的次序。所以我们
可以用builder pattern来generate所有这些步骤。
写个code(看下面link),欢迎各位排砖。
http://www.ideone.com/KsDCV