Redian新闻
>
Amazon OO Design 经典题的思考
avatar
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
avatar
a*c
2
I think you're looking into this too deep and over-thinking it. For
this
kind of interview questions, I don't think the interviewer is looking
for "
the answer", but rather an answer. From there you pretty much just have
to
wing it and think on the fly. There's always something extra you can
add to
your program to account for some special case. For example, you can add
something to account for walk-in customers, pricing information or even
hourly, weekly, or monthly rental. There's just too many possibilities
and
it all boils down to how much you know about the hotel industry, either
beforehand or from what your interviewer tells you. But all of that
is beside the point. Let your interviewer raise the questions and tell
him/
her how you can adapt your program to these requirement changes. Most
of
these additional design requirements don't really add any complexity
to the
problem, it's just tedious coding work. Seriously, for most general
programming interview questions you don't already know the answers to,
OO design is pretty low on the difficulty scale.
avatar
l*n
3
恩,多谢指点。不过还是想问问面试的时候,遇到这种问题怎样回答才算是OK的design
我想面试官不会只考你会不会写class,会不会用inheritance, virtual function之类
的吧。
不管这类问题再怎么open,总还是有一些考察的侧重点的吧。
avatar
j*u
4
what is your point?
To me, no design is bad, over design is worse.
I tend to prefer design questions over pure algorithms coding for especially
experinced developers during the interview.It is more important to work, in
most of the cases.
It is easy to differentiate the level of candidates.I would not want to hire
who just coding and coding without thinking.
But keep the design simple. Tell the interviewer why you choose to to this
way rather than that way. Tell him what's your trade-off, why there's a
better solution in terms of beaty design but you don't use it.
Let's take an simple example in your solution. Why do you have the abstract
room class and have different rooms inherited from it? It's not because you
want to use factory pattern, but rather you expect it is a "hot spot" that
is very likely to change, i.e. new type of rooms. Facroty pattern is just
helping you to go there. If there will never or unlikely to have new type of
rooms, there's no need to use patterns which adds complexity.
A good design is beautiful and flexible. Keeping it simple, kowning the
limitations, doing refactory all the time, preparing for the predicable
changes but don't do everything upfront.
Patterns help you to think, to communicate and to achieve a flexible design.
But use it when it's really needed.
hope these are helpful...

【在 a***c 的大作中提到】
: I think you're looking into this too deep and over-thinking it. For
: this
: kind of interview questions, I don't think the interviewer is looking
: for "
: the answer", but rather an answer. From there you pretty much just have
: to
: wing it and think on the fly. There's always something extra you can
: add to
: your program to account for some special case. For example, you can add
: something to account for walk-in customers, pricing information or even

avatar
s*t
5
Nice explanation!!

especially
in
hire

【在 j*****u 的大作中提到】
: what is your point?
: To me, no design is bad, over design is worse.
: I tend to prefer design questions over pure algorithms coding for especially
: experinced developers during the interview.It is more important to work, in
: most of the cases.
: It is easy to differentiate the level of candidates.I would not want to hire
: who just coding and coding without thinking.
: But keep the design simple. Tell the interviewer why you choose to to this
: way rather than that way. Tell him what's your trade-off, why there's a
: better solution in terms of beaty design but you don't use it.

avatar
a*c
6
so do you agree with me or do you not? I'm kind of confused. I think you
just
rephrased the stuff I said.

especially
in
hire

【在 j*****u 的大作中提到】
: what is your point?
: To me, no design is bad, over design is worse.
: I tend to prefer design questions over pure algorithms coding for especially
: experinced developers during the interview.It is more important to work, in
: most of the cases.
: It is easy to differentiate the level of candidates.I would not want to hire
: who just coding and coding without thinking.
: But keep the design simple. Tell the interviewer why you choose to to this
: way rather than that way. Tell him what's your trade-off, why there's a
: better solution in terms of beaty design but you don't use it.

avatar
j*u
7
it seems we were talking about similiar thing in different perspective
I wasn't rephrasing just didn't read yours carefully - sorry
I just read again

【在 a***c 的大作中提到】
: so do you agree with me or do you not? I'm kind of confused. I think you
: just
: rephrased the stuff I said.
:
: especially
: in
: hire

avatar
s*n
8
search online. you will find if this is questioned in phone interview,
basically they just wnat to make sure you know the abc of OOD. very basic.
However, there will be other issues can be discussed besides OOD like the
data structures/algorithm used for example to query : which room is free
during some day to some day, efficiently. It would be nice to use design
patterns. However, I don't see it is obvious for this question.
avatar
C*E
9
牛。

【在 s*****n 的大作中提到】
: search online. you will find if this is questioned in phone interview,
: basically they just wnat to make sure you know the abc of OOD. very basic.
: However, there will be other issues can be discussed besides OOD like the
: data structures/algorithm used for example to query : which room is free
: during some day to some day, efficiently. It would be nice to use design
: patterns. However, I don't see it is obvious for this question.

avatar
j*0
10
Then what is the ABC of OOD? Thank you.

【在 s*****n 的大作中提到】
: search online. you will find if this is questioned in phone interview,
: basically they just wnat to make sure you know the abc of OOD. very basic.
: However, there will be other issues can be discussed besides OOD like the
: data structures/algorithm used for example to query : which room is free
: during some day to some day, efficiently. It would be nice to use design
: patterns. However, I don't see it is obvious for this question.

avatar
a*c
11
I think the trick is to start small, then literally work with your
interviewer. Ask questions whenever you need clarification, assume nothing;
show him your design process rather than your design. Be more proactive, it'
s
better to ask questions than getting hints from him/her because you
overlooked
something. For the hotel reservation problem, having the basic stuff, e.g.
room type, availability, etc is plenty good enough to get the discussion
going, from there, you could start asking questions like how is the system
used, is it run at multiple sites (think concurrency, database)? does it
need
an interface for third party online reservation agencies? maybe yet another
interface for other travel agencies? is commission handled by your system as
well? what information should be made available to these third parties?

design

【在 l********n 的大作中提到】
: 恩,多谢指点。不过还是想问问面试的时候,遇到这种问题怎样回答才算是OK的design
: 我想面试官不会只考你会不会写class,会不会用inheritance, virtual function之类
: 的吧。
: 不管这类问题再怎么open,总还是有一些考察的侧重点的吧。

avatar
l*n
12
Great! 多谢各位的讨论和idea. 现在有点知道该怎么回答这类问题了
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。