Pragmatic Coder

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

          置頂隨筆

          歡迎大家加入:
          1.讀書(shū)心得交流,大家可以相約一起讀書(shū)互相交流
          2.技術(shù)交流,任何與設(shè)計(jì)模式相關(guān),包括重構(gòu),測(cè)試驅(qū)動(dòng),敏捷開(kāi)發(fā)等,開(kāi)發(fā)中遇到的問(wèn)題。
          要求至少發(fā)表過(guò)一篇關(guān)于設(shè)計(jì)模式的文章才可以加入。
          http://group.qq.com/group_index.shtml?funcid=1&groupid=26227899
          友情提示:如果你只打算“聽(tīng)”,不打算“說(shuō)”或者“問(wèn)”,對(duì)你的幫助會(huì)小的多。

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

          2006年9月16日

          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.

          結(jié)合使用JUnit和Cobertura可能是不錯(cuò)的選擇。

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

          2006年9月3日

          〔本站副站發(fā)布,主站:designpatterns.cnblogs.com〕

          ○。背景與背景知識(shí)

          這是昨天在QQ上舉的一個(gè)例子。本文并不是從頭講Factory Method模式,僅對(duì)其實(shí)現(xiàn)細(xì)節(jié)進(jìn)行討論。關(guān)于這個(gè)模式可以參考wayfarer呂震宇的文章。

          一。分析

          因?yàn)镕actory Method(大寫的Factory Method表示模式名稱)模式的一個(gè)目的就是使得創(chuàng)建行為(factory method)(小寫的factory method表示創(chuàng)建行為方法)抽象化,由子類實(shí)現(xiàn),從而容易擴(kuò)展。一個(gè)純的Factory Method模式,其factory method是不能用static來(lái)實(shí)現(xiàn)的。通常Simple Factory模式才會(huì)由static來(lái)實(shí)現(xiàn)。

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

          引用Walkdan的回復(fù):

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

          1. 聲明構(gòu)造子

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

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

          2. 注冊(cè)構(gòu)造子

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

          3.?Simple Factory中創(chuàng)建對(duì)象?

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

          構(gòu)造子其實(shí)就是Factory Method。這樣, 通過(guò)注冊(cè)構(gòu)造子,3.中原來(lái)的switch()過(guò)程被消除,依賴關(guān)系隨之解除。其中的Register(), Find()容易理解,方法就不寫了。

          新的類型可通過(guò)新注冊(cè)構(gòu)造子來(lái)實(shí)現(xiàn)擴(kuò)展。當(dāng)然2中的注冊(cè)代碼僅僅是示例,完全可以放到配置文件中去實(shí)現(xiàn),比如:

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

          其ILightCreator繼承體系,是Factory Method模式。LightSimpleFactory和creators的關(guān)系Walkdan沒(méi)有說(shuō)明,也可實(shí)現(xiàn)為Strategy模式。

          二。引申問(wèn)題

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

          三。結(jié)論

          一個(gè)純的Factory Method模式其factory method不可以用static實(shí)現(xiàn),通過(guò)與其他模式結(jié)合使用可以進(jìn)行變通。而且通常情況下Factory Method模式都會(huì)與其他模式相結(jié)合。Abstract Factory模式通常就是用Factory Method模式來(lái)實(shí)現(xiàn)。

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

          2006年9月2日

          歡迎大家加入:
          1.讀書(shū)心得交流,大家可以相約一起讀書(shū)互相交流
          2.技術(shù)交流,任何與設(shè)計(jì)模式相關(guān),包括重構(gòu),測(cè)試驅(qū)動(dòng),敏捷開(kāi)發(fā)等,開(kāi)發(fā)中遇到的問(wèn)題。
          要求至少發(fā)表過(guò)一篇關(guān)于設(shè)計(jì)模式的文章才可以加入。
          http://group.qq.com/group_index.shtml?funcid=1&groupid=26227899
          友情提示:如果你只打算“聽(tīng)”,不打算“說(shuō)”或者“問(wèn)”,對(duì)你的幫助會(huì)小的多。

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

          2006年8月20日

          本書(shū)中的大多數(shù)原則源于少數(shù)幾條基本原則。

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

          2.代碼應(yīng)該被重用,而不是被拷貝。

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

          4.錯(cuò)誤應(yīng)該盡早被檢測(cè)出來(lái),理想情況下是在編譯時(shí)刻。

          posted @ 2006-08-20 14:12 肖鵬 閱讀(205) | 評(píng)論 (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) | 評(píng)論 (0)編輯 收藏

          2006年7月27日

          Ant 對(duì)文件名是區(qū)分大小寫的

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

          2006年7月18日

          0.引子
          通常一個(gè)TreeNode (DefaultMutableTreeNode)如果沒(méi)有子結(jié)點(diǎn),它的節(jié)點(diǎn)顯示為不可展開(kāi)的節(jié)點(diǎn),有時(shí)候我們希望即使該節(jié)點(diǎn)暫時(shí)沒(méi)有裝載子結(jié)點(diǎn)仍然顯示為可展開(kāi)的節(jié)點(diǎn)(如圖ParentNode.jpg)。比如,當(dāng)加載子結(jié)點(diǎn)比較耗時(shí)或者需要?jiǎng)討B(tài)刷新,可以延時(shí)加載子結(jié)點(diǎn),即在展開(kāi)節(jié)點(diǎn)的時(shí)候再去裝載子結(jié)點(diǎn)。
          1.由來(lái)及解決方案
          該屬性取決于,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設(shè)定
          ???  /**
                * 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時(shí)可以只設(shè)定一次
          ??????  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);

          而在調(diào)用model.nodeStructureChanged(node)前后要分別設(shè)置為false和true。這是因?yàn)閚odeStructureChanged會(huì)自動(dòng)的試圖展開(kāi)節(jié)點(diǎn)node,如果isLeaf返回為false。試想,asksAllowsChildren返回true,則一個(gè)結(jié)點(diǎn)即使沒(méi)有子結(jié)點(diǎn),只要設(shè)置了allowsChildren,對(duì)應(yīng)的treeNodeModel的isLeaf就會(huì)返回true。然而當(dāng)自動(dòng)展開(kāi)節(jié)點(diǎn)的時(shí)候發(fā)現(xiàn)并沒(méi)有子結(jié)點(diǎn),當(dāng)然就會(huì)把圖標(biāo)ParentNode.jpg去掉了。而先把a(bǔ)sksAllowsChildren設(shè)為false,就不會(huì)去試圖展開(kāi)該節(jié)點(diǎn),然后設(shè)為true,該節(jié)點(diǎn)就會(huì)顯示為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 肖鵬 閱讀(559) | 評(píng)論 (0)編輯 收藏

          2006年7月17日

          1.
          ???????vectorObj.trimToSize();???//(optional) insure you won't have empty elements
          ???????ArrayObj[]?objs = new ArrayObj[vectorObj.size()];
          ???????vectorObj.toArray(objs);
          ???????return objs.
          2.
          ?????? Object[] tempObjectArray = vectorObj.toArray();
          ?????? ArrayObj[] objs = new ArrayObj[ tempObjectArray.size() ];
          ?????? System.arraycopy (tempObjectArray, 0, objs, 0, objs.size() );
          ???????return objs;

          posted @ 2006-07-17 17:07 肖鵬 閱讀(264) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 辽阳市| 南雄市| 台东市| 疏附县| 启东市| 广宗县| 城步| 富源县| 丹江口市| 永泰县| 平远县| 金沙县| 三河市| 丰都县| 柯坪县| 邯郸县| 武强县| 墨玉县| 洛隆县| 双流县| 金堂县| 邹城市| 嫩江县| 阿巴嘎旗| 清原| 涟源市| 漳州市| 隆德县| 阜康市| 汤阴县| 玛沁县| 兴宁市| 陵水| 肇州县| 哈密市| 万全县| 罗定市| 安新县| 肇庆市| 古蔺县| 康平县|