Design question: does the Phone dial the PhoneNumb

2019-02-09 16:19发布

This is re-posted from something I posted on the DDD Yahoo! group.

All things being equal, do you write phone.dial(phoneNumber) or phoneNumber.dialOn(phone)? Keep in mind possible future requirements (account numbers in addition to phone numbers, calculators in addition to phones).

The choice tends to illustrate how the idioms of Information Expert, Single Responsibility Principle, and Tell Don't Ask are at odds with each other.

phoneNumber.dialOn(phone) favors Information Expert and Tell Don't Ask, while phone.dial(phoneNumber) favors Single Responsibility Principle.

If you are familiar with Ken Pugh's work in Prefactoring, this is the Spreadsheet Conundrum; do you add rows or columns?

标签: oop
13条回答
等我变得足够好
2楼-- · 2019-02-09 16:50

I wouldn't have phone number as a class at all as it does not have any behavior, it's just a data element.

查看更多
登录 后发表回答