1

My background: I am a recent graduate looking for job in software industry. Question: I was in recent interview with one of the software companies and was asked to draw UML diagram for a bank system which shows 2 accounts e.g. saving and checking and they have different way of calculating interest.

My Solution: I made an Account class an abstract class.
Like this: public abstract class Account{ ...... } This class have 2 methods defined in it deposit() and withdraw() which is common to any account type. another method CalculateInterest() which is abstract method.

2 classes saving and checking which extends account class and implement Account class. e.g: public class Saving extends Account { ... }

I added other class to top off UML like Bank and bank location but this did not satisfied interviewer and he wanted me to implement whole process as INTERFACES which i quite did not understood very well. I tried extracting same information but it did not pleased interviewer.

Any information that people out here can share will help me a lot in understanding design and further how to approach interviews.
I know their are lot of design patterns which are out there but when he mentioned about specific interfaces I was not sure how to approach that.

4

1 回答 1

3

在正常的银行业务流程中,您已经给出了很好的答案。然而,对于复杂的银行业务需求,他们将需要更多的模块化设计,而这正是界面大放异彩的地方。

在您的基本设计中,您会说:

  • 所有账户都可以存款
  • 所有账户都可以提款
  • 所有账户都可以计算利息

用您当前的设计说,如果要求是:

  1. account只能创建一个类型deposit。不能提现,只能关闭(如限时充值)
  2. account只能创建一个类型withdraw。开的时候说你deposit有钱account,那你只能withdraw,最后关了
  3. 创建一个accountcan only的类型CalculateInterest,而不是depositor withdraw
  4. 等等

在您的设计中,您可以继承基Account类,并为每个不受支持的操作(存款等)抛出未实现的异常。但是,(请纠正我)这违反了 LSP 并冒着运行时异常的风险。

使用基于接口,您需要声明一些接口:

  • IAccount(具有余额、用户 ID 等基本属性)
  • IDepositable : IAccount
  • IWithdrawable : IAccount
  • IClosable : IAccount
  • ICalculateInterestable : IAccount

然后对于要求,您可以声明一个类:

  1. 实现 IDepositable、IClosable、ICalculateInterestable
  2. 实现 IWithdrawable、IClosable、ICalculateInterestable
  3. 实现 IClosable、ICalculateInterestable

这可能不是最简洁的设计,但应该足以满足大部分银行业务的需求。

于 2013-06-05T02:51:42.807 回答