我看到的最多被违反的原则是“命令,不要去询问(Tell, Don’t Ask)”原则。这个原则讲的是,一个对象应该命令其它对象该做什么,而不是去查询其它对象的状态来决定做什么(查询其它对象的状态来决定做什么也被称作‘功能嫉妒(Feature Envy)’)。
这篇文章里有个很生动的例子,我至今记忆犹新:
if (person.getAddress().getCountry() == “Australia”) {
这违反了得墨忒耳定律,因为这个调用者跟Person过于亲密。它知道Person里有一个Address,而Address里还有一个country。它实际上应该写成这样:
if (person.livesIn(“Australia”)) {
非常的明了。今天我又看到一个关于“Tell, Don’t Ask”原则的文章,里面提供了4个关于这个原则的例子,都很有价值。
例一
不好:
<% if current_user.admin? %><%= current_user.admin_welcome_message %><% else %><%= current_user.user_welcome_message %><% end %>
好:
<%= current_user.welcome_message %>
例二
不好:
def check_for_overheating(system_monitor)if system_monitor.temperature > 100system_monitor.sound_alarmsendend
好:
system_monitor.check_for_overheatingclass SystemMonitordef check_for_overheatingif temperature > 100sound_alarmsendendend
例三
不好:
class Postdef send_to_feedif user.is_a?(TwitterUser)user.send_to_feed(contents)endendend
好:
class Postdef send_to_feeduser.send_to_feed(contents)endendclass TwitterUserdef send_to_feed(contents)twitter_client.post_to_feed(contents)endendclass EmailUserdef send_to_feed(contents)# no-op.endend
例四
不好:
def street_name(user)if user.addressuser.address.street_nameelse'No street name on file'endend
好:
def street_name(user)user.address.street_nameendclass Userdef address@address || NullAddress.newendendclass NullAddressdef street_name'No street name on file'endend
好的面向对象编程是告诉对象你要做什么,而不是询问对象的状态后根据状态做行动。数据和依赖这些数据的操作都应该属于同一个对象。
命令,不要去询问!
分享到:
相关推荐
2. Ask him 变为 "Don't ask him." 3. Please wait for her 变为 "Please don't wait for her." 4. Read the book carefully 变为 "Don't read the book carelessly." 5. Sit under the tree, please 变为 "Don't ...
在IT行业中,"Tell Don't Ask"原则是一个重要的设计哲学,尤其在面向对象编程中被广泛推崇。这个原则源于Smalltalk社区,由Martin Fowler在其著作《重构:改善既有代码的设计》中进行了阐述。"Tell Don't Ask"的核心...
for sth.** - 请求或要求得到某物,如"Don’t always ask your parents for money."告诫不要总是向父母要钱。 5. **ask/tell sb. how to do sth.** - 教导别人如何做某事,如"I asked /told the man how to get to...
祈使句通常用来表达命令、请求、劝告、警告或禁止等,其基本结构是动词原形加上其他成分,或者以"Please"或"Don't/Never"开头。 1. **祈使句的基本形式**: - 简单祈使句:如 "Go and wash your hands." 这种结构...
- Don't ask me to help you. You must rely on yourself. (不要让我帮你!你必须要靠自己。) 6. **Tell sb. to do/ tell sb. not to do**:这两个结构表示“告诉某人做/不要做某事”。 - Let me tell you how ...
But don’t be late next time. / Go and sit down. Come earlier next time. 提醒学生回到座位,并鼓励他们以后准时。 18. 调整坐姿:Sit straight, please. Take off your cap, please. 提醒学生保持良好的坐姿...
But don’t be late next time." 让迟到的学生回到座位并提醒下次不要迟到。 18. **坐姿与仪态** - "Sit straight, please. Take off your cap, please." 提醒学生保持良好的坐姿并纠正不适宜的着装。 19. **分享...
例如,第1题"“Don't play games in the classroom, ” the monitor said to us." 转换成 "The monitor told us not to play games in the classroom." 这里时态由一般现在时变为一般过去时,祈使句"Don't" 变成了 ...
- Don’t ask me to help you; you must rely on yourself. (不要让我帮你的忙!你必须要自己解决问题。) - My mother asks us not to eat anything before we go to bed. (妈妈让我们睡觉前不吃东西。) 6. tell ...
六、“Tell, Don't Ask”原则 该原则鼓励我们不要询问对象的状态,而是告诉对象需要做什么。这样可以减少对象间的耦合,提高代码的灵活性。 七、编程习惯和实践 良好的编程习惯,如适当的注释、文档编写、版本控制...
- 鼓励多提问:"Don't be afraid to ask a lot of questions." - 对他人的到来表示欢迎:"I hope you'll be happy working here." 6. **词汇词性与搭配**: - "Isn't it impolite to call people by their first...
- "Sorry, I don’t know. I’m new here, too."(对不起,我不知道。我也是刚到这儿的。) - "I'm sorry, I'm not sure. You'd better ask the policeman over there."(对不起,我不能确定,你最好问那边的警察...
- 祈使句引导的宾语从句,肯定形式为"ask/tell… +sb. +to +v.",否定形式为"ask/tell/…+sb.+ not to + v.",如"Could you tell me how often you go to the library?" 或者 "Ask him not to forget the meeting....
- Sorry, I don’t know. I’m new here, too. (对不起,我不知道。我也是刚到这儿的。) - I'm sorry, I'm not sure. You'd better ask the policeman over there. (对不起,我不能确定,你最好问那边的警察。...
- 直接引语:"Don’t make a mess in the kitchen," 她对他说。 - 间接引语:She told him not to make a mess in the kitchen. 3. **建议语气**:建议性的祈使句常变为 "suggest + 宾语从句" 或 "suggest + 动...
7. 建议或禁止类,直接引语中的"Don't"在间接引语中保持不变,即`said, "Don't smoke any more."` 8. 请求类,祈使句变为let sb. do结构,时间状语today不变,即`Let me go home earlier today.` 9. 请求类,ask ...
B.I don’t know.C.Sorry, I am new here.D.You should take Bus No. 10.【答案】C【解析】【详解】考查情景交际。句意:---打扰一下,你能告诉我最近的邮局怎么走吗?---对不起,我是新来的。问路时如果对方不...