Pragmatic Coder

          best practices
          posts - 8, comments - 0, trackbacks - 0, articles - 0

          2006年7月18日

          avg_3000 wrote:
          > Hi,
          >
          > Can you suggest a tutorial site for junit and jcoverage that covers
          > from the grassroot level since I have no idea on how to use this tools
          > in java.

          jcoverage is as good as dead. Use Cobertura, which is its replacement.
          Cobertura has documentation for running it in Ant; it's even easier in
          Maven.
          Take a look at <http://cobertura.sourceforge.net/anttaskreference.html>,
          since that's what Google would probably have pulled up if you'd searched.

          結合使用JUnit和Cobertura可能是不錯的選擇。

          posted @ 2006-09-16 09:59 肖鵬 閱讀(249) | 評論 (0)編輯 收藏

          〔本站副站發布,主站:designpatterns.cnblogs.com〕

          ○。背景與背景知識

          這是昨天在QQ上舉的一個例子。本文并不是從頭講Factory Method模式,僅對其實現細節進行討論。關于這個模式可以參考wayfarer呂震宇的文章。

          一。分析

          因為Factory Method(大寫的Factory Method表示模式名稱)模式的一個目的就是使得創建行為(factory method)(小寫的factory method表示創建行為方法)抽象化,由子類實現,從而容易擴展。一個純的Factory Method模式,其factory method是不能用static來實現的。通常Simple Factory模式才會由static來實現。

          但是,一個Factory擁有一個static的創建方法是很誘人的。用戶在任何時候只要用類名(Factory的類名)既可以創建出需要的Product。wayfarer?的文章最后給出的DotNet的實現部分就是這樣的例子。呂震宇的另一篇文章?(關于Simple Factory模式)的回復中Walkdan給出的解決方案也是這樣的。從本質上,這兩個例子都不是純的Factory Method模式或者Simple Factory模式,而是Simple Factory模式和Factory Method模式的組合應用。

          引用Walkdan的回復:

          Simple Factory最大的好處是把變化集中到了一處。另外,LightSimpleFactory.Create()里面的依賴關系也可以消除,我的做法是:

          1. 聲明構造子

          public interface?ILightCreator
          {
          ????Light?Create();
          }

          public class?BulbLightCreator:?ILightCreator
          {
          public?Light?Create()
          ????{
          return new?BulbLight();
          ????}
          }
          .

          2. 注冊構造子

          creators.Register("Bulb",?new?BulbLightCreator());
          creators.Register("Tube",?new?TubeLightCreator());
          .

          3.?Simple Factory中創建對象?

          public class?LightSimpleFactory.Create(string?lightType)
          {
          ????ILightCreator?creator?=?creators.Find(lightType);
          return?creator.Create();
          }

          構造子其實就是Factory Method。這樣, 通過注冊構造子,3.中原來的switch()過程被消除,依賴關系隨之解除。其中的Register(), Find()容易理解,方法就不寫了。

          新的類型可通過新注冊構造子來實現擴展。當然2中的注冊代碼僅僅是示例,完全可以放到配置文件中去實現,比如:

          <lightCreators>
          <add?name="Bulb"?type="BulbLightCreator,"/>
          <add?name="Tube"?type="TubeLightCreator,"/>
          </lightCreators>

          其ILightCreator繼承體系,是Factory Method模式。LightSimpleFactory和creators的關系Walkdan沒有說明,也可實現為Strategy模式。

          二。引申問題

          1. 模式中factory method一定要是抽象的嗎? NO.你可以定義一個默認的方法,生產某種Product。這樣即使Factory沒有子類也可以生產這種默認產品。通常這種情況下Factory是一個接口,然后定義一個BasicFactory來生產BasicProduct。其他的Factory由BasicFactory派生。注意,往往Factory和Product是平行的繼承結構。
          2. Factory Method解耦了client和concreteProduct,是不是又引入了client和concreteFactory的耦合? 這個問題很好,答案是可能(《head first design patterns》一書中的例子就有這個問題),但是client端不應該依賴concreteFactory。如果依賴concreteFactory,那真是畫蛇添足了(見wayfarer的文章)。
          3. Factory Method模式的好處有哪些?
            1. 封裝了對象的創建。對象的創建如果使用new來完成,則與實現類(concreteProduct)是緊耦合的。Factory Method的創建行為factory method,返回Product接口,從而解耦了client和concreteProduct。
            2. 易于擴展。相對于Simple Factory模式,Factory Method模式可以通過派生進行擴展。

          三。結論

          一個純的Factory Method模式其factory method不可以用static實現,通過與其他模式結合使用可以進行變通。而且通常情況下Factory Method模式都會與其他模式相結合。Abstract Factory模式通常就是用Factory Method模式來實現。

          posted @ 2006-09-03 14:21 肖鵬 閱讀(426) | 評論 (0)編輯 收藏

          歡迎大家加入:
          1.讀書心得交流,大家可以相約一起讀書互相交流
          2.技術交流,任何與設計模式相關,包括重構,測試驅動,敏捷開發等,開發中遇到的問題。
          要求至少發表過一篇關于設計模式的文章才可以加入。
          http://group.qq.com/group_index.shtml?funcid=1&groupid=26227899
          友情提示:如果你只打算“聽”,不打算“說”或者“問”,對你的幫助會小的多。

          posted @ 2006-09-02 22:05 肖鵬 閱讀(328) | 評論 (0)編輯 收藏

          本書中的大多數原則源于少數幾條基本原則。

          1.清晰性和簡介性是最為重要的:一個模塊的用戶永遠也不應該被模塊的行為所迷惑(那樣就不清晰了);模塊要盡可能的小,但又不能太小〔術語模塊(module)在本書中的用法,是指任何可重用的軟件組件,從單個方法,到包含多個包的復雜系統都可以是一個模塊〕。

          2.代碼應該被重用,而不是被拷貝。

          3.模塊之間的相依性應該盡可能降低到最小。

          4.錯誤應該盡早被檢測出來,理想情況下是在編譯時刻。

          posted @ 2006-08-20 14:12 肖鵬 閱讀(205) | 評論 (0)編輯 收藏

          This is a temporary post that was not deleted. Please delete this manually. (e6a97894-4dd8-4eed-8ba1-b291c8b2bc96)

          posted @ 2006-08-20 14:02 肖鵬 閱讀(134) | 評論 (0)編輯 收藏

          Ant 對文件名是區分大小寫的

          posted @ 2006-07-27 16:23 肖鵬 閱讀(226) | 評論 (0)編輯 收藏

          0.引子
          通常一個TreeNode (DefaultMutableTreeNode)如果沒有子結點,它的節點顯示為不可展開的節點,有時候我們希望即使該節點暫時沒有裝載子結點仍然顯示為可展開的節點(如圖ParentNode.jpg)。比如,當加載子結點比較耗時或者需要動態刷新,可以延時加載子結點,即在展開節點的時候再去裝載子結點。
          1.由來及解決方案
          該屬性取決于,DefaultTreeModel的isLeaf方法
          ?
          ??? /** 
               * Returns whether the specified node is a leaf node.
               * The way the test is performed depends on the
               * <code>askAllowsChildren</code> setting.
               *
               * @param node the node to check
               * @return true if the node is a leaf node
               *
               * @see #asksAllowsChildren
               * @see TreeModel#isLeaf
               */
          publicboolean isLeaf(Object node) {
                  if(asksAllowsChildren)
          return !((TreeNode)node).getAllowsChildren();
                  return ((TreeNode)node).isLeaf();
              }
          而asksAllowsChildren由方法setAsksAllowsChildren設定
          ???  /**
                * Sets whether or not to test leafness by asking getAllowsChildren()
                * or isLeaf() to the TreeNodes.  If newvalue is true, getAllowsChildren()
                * is messaged, otherwise isLeaf() is messaged.
                */
          publicvoid setAsksAllowsChildren(boolean newValue) {
                  asksAllowsChildren = newValue;
              }
          注意,在setRoot時可以只設定一次
          ??????  model.setRoot(root);
          
                  /*
                   * Since nodes are added dynamically in this application, the only true
                   * leaf nodes are nodes that don't allow children to be added. (By
                   * default, askAllowsChildren is false and all nodes without children
                   * are considered to be leaves.)
                   *
                   * But there's a complication: when the tree structure changes, JTree
                   * pre-expands the root node unless it's a leaf. To avoid having the
                   * root pre-expanded, we set askAllowsChildren *after* assigning the
                   * new root.
                   */
                  model.setAsksAllowsChildren(true);

          而在調用model.nodeStructureChanged(node)前后要分別設置為false和true。這是因為nodeStructureChanged會自動的試圖展開節點node,如果isLeaf返回為false。試想,asksAllowsChildren返回true,則一個結點即使沒有子結點,只要設置了allowsChildren,對應的treeNodeModel的isLeaf就會返回true。然而當自動展開節點的時候發現并沒有子結點,當然就會把圖標ParentNode.jpg去掉了。而先把asksAllowsChildren設為false,就不會去試圖展開該節點,然后設為true,該節點就會顯示為ParentNode.jpg???

          ??? /**
               * Determines whether or not this node is allowed to have children. 
               * If <code>allows</code> is false, all of this node's children are
               * removed.
               * <p>
               * Note: By default, a node allows children.
               *
               * @param	allows	true if this node is allowed to have children
               */
          publicvoid setAllowsChildren(boolean allows) {
          	if (allows != allowsChildren) {
          	    allowsChildren = allows;
          	    if (!allowsChildren) {
          		removeAllChildren();
          	    }
          	}
              }
                  node.removeAllChildren();
                  DefaultTreeModel model = (DefaultTreeModel) cardTreeView.getModel();
          
                  /*
                   * To avoid having JTree re-expand the root node, we disable
                   * ask-allows-children when we notify JTree about the new node
                   * structure.
                   */
          
                  model.setAsksAllowsChildren(false);
                  model.nodeStructureChanged(node);
                  model.setAsksAllowsChildren(true);

          posted @ 2006-07-18 09:57 肖鵬 閱讀(560) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 山东| 浙江省| 绥滨县| 色达县| 临泉县| 合水县| 呼和浩特市| 察哈| 苏尼特右旗| 阜南县| 于都县| 蓬莱市| 鄱阳县| 响水县| 方山县| 德令哈市| 都安| 慈利县| 济南市| 保亭| 乌苏市| 惠州市| 马边| 铜鼓县| 昂仁县| 灵寿县| 台江县| 浮山县| 郁南县| 兰溪市| 龙陵县| 阿瓦提县| 会昌县| 项城市| 靖西县| 黎平县| 荣昌县| 南雄市| 公安县| 临海市| 巴彦淖尔市|