guangnian0412's BLOG

          Java in my life

          常用鏈接

          統(tǒng)計

          積分與排名

          我關(guān)注的Blog

          最新評論

          笛米特法則詳解(the Law of Demeter or Principle of Least Knowledge)

          ?? The Law of Demeter和 Principle of Least Knowledge講的都是一回事,是說一個軟件實體要盡可能的只與和它最近的實體進(jìn)行通訊。通常被表述為:talk only to your immediate friends ( 只和離你最近的朋友進(jìn)行交互)。?

          ?? “talk”,其實就是對象間方法的調(diào)用。這條規(guī)則表明了對象間方法調(diào)用的原則:

          (1)?????? 調(diào)用對象本身的方法;

          (2)?????? 調(diào)用通過參數(shù)傳入的對象的方法;

          (3)?????? 在方法中創(chuàng)建的對象的方法;

          (4)?????? 所包含對象的方法。

          上面的4點看起來有點別扭,下面通過一個具體的例子,就可以對上述4guideline有進(jìn)一步感性的認(rèn)識:

          ?1?public?class?Car?{
          ?2???Engine?engine;
          ?3???
          ?4???public?Car()?{
          ?5?????//initialize?engine,etc.
          ?6?}
          ?7?
          ?8?public?void?start(Key?key)?{
          ?9???Doors?doors?=?new?Doors();
          10???boolean?authorized?=?key.turns();
          11???
          12???if(authorized)?{
          13?????engine.start();
          14?????updateDashboardDisplay();
          15?????doors.lock();
          16?}
          17?
          18?public?void?updateDashboardDisplay()?{
          19???//update?display
          20}
          21
          22?}

          下面對start()方法中的語句進(jìn)行分析:

          第10行-key.turns():符合上述的第(2)條,key對象是通過參數(shù)傳入start()方法的。

          第13行-engine.start():符合上述的第(4)條,engine對象是包含在Car的對象之中的。

          第14行-UpdateDashboardDisplay():符合上述的第(1)條,UpdateDashboardDisplay()方法是Car對像自身的方法。

          第15行-doors.lock():符合上述的第(3)條,doors對象是在start()方法中創(chuàng)建的對象。

          接下來看一個違反Principle of Least Knowledge的例子:

          1?public?float?getTemp()?{
          2???Thermometer?thermometer?=?station.getThermometer();
          3???return?thermometer.getTemperature();
          4?}

          上面的方法中station對象是immediate friends。但是上面的代碼卻從station對象中返回了一個Thermometer對象,然后調(diào)用了thermometer對象的getTemperature()方法,違反了Principle of Least Knowledge

          下面對上面的方法作出符合Principle of Least Knowledge的改進(jìn):

          1?public?float?getTemp()?{
          2???return?station.?getTemperature();
          3?}

          我們在Station類中添加一個方法getTemperature()。這個方法將調(diào)用Station類中含有的Thermometer對象的getTemperature()。這樣getTemp()方法就只知道Station對象而不知道Thermometer對象。

          總結(jié):笛米特法則告訴我們要盡量只和離自己最近的對象進(jìn)行交互。離自己最近的對象包括:自身包含的對象,方法中創(chuàng)建的對象,通過參數(shù)傳進(jìn)的對象,還有自己本身。
          ?

          posted on 2006-06-19 19:44 guangnian 閱讀(4596) 評論(5)  編輯  收藏 所屬分類: OO設(shè)計原則

          評論

          # re: 笛米特法則詳解(the Law of Demeter or Principle of Least Knowledge)[未登錄] 2007-11-19 10:51 訪客

          這個還叫詳解?不要為了提高點擊率隨便命名,充其量也只能是實例說明。誤導(dǎo)~!  回復(fù)  更多評論   

          # re: 笛米特法則詳解(the Law of Demeter or Principle of Least Knowledge)[未登錄] 2007-12-19 17:34 abc

          介紹的也太簡單了  回復(fù)  更多評論   

          # re: 笛米特法則詳解(the Law of Demeter or Principle of Least Knowledge) 2008-01-29 16:48 nf

          我以為介紹得不錯啊,難道長篇大論LS的才滿意嗎?  回復(fù)  更多評論   

          # re: 笛米特法則詳解(the Law of Demeter or Principle of Least Knowledge) 2009-07-05 14:53 fnd

          我也認(rèn)為寫的不錯,推一下  回復(fù)  更多評論   

          # re: 笛米特法則詳解(the Law of Demeter or Principle of Least Knowledge)[未登錄] 2013-10-20 18:59 andrew

          謝謝,學(xué)習(xí)了  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 麦盖提县| 广元市| 贡觉县| 都江堰市| 雷波县| 西和县| 临沭县| 固镇县| 咸丰县| 锦屏县| 泾川县| 乌审旗| 尚志市| 阿城市| 青州市| 鹤峰县| 新泰市| 新野县| 江源县| 乌兰浩特市| 株洲市| 灯塔市| 临夏县| 石台县| 平安县| 双流县| 双辽市| 临高县| 五大连池市| 元江| 桐乡市| 滦平县| 资兴市| 拉孜县| 文水县| 镶黄旗| 深泽县| 崇文区| 沽源县| 黄大仙区| 波密县|