??xml version="1.0" encoding="utf-8" standalone="yes"?>
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.
l合使用JUnit和Cobertura可能是不错的选择?/p>
○。背景与背景知识
q是昨天在QQ上D的一个例子。本文ƈ不是从头讲Factory Method模式Q仅对其实现l节q行讨论。关于这个模式可以参?a target="_blank">wayfarer?a target="_blank">吕震?/a>的文章?/p>
一。分?/p>
因ؓFactory MethodQ大写的Factory Method表示模式名称Q模式的一个目的就是得创为(factory methodQ(写的factory method表示创徏行ؓҎ(gu)Q抽象化Q由子类实现Q从而容易扩展。一个纯的Factory Method模式Q其factory method是不能用static来实现的。通常Simple Factory模式才会由static来实现?/p>
但是Q一个Factory拥有一个static的创建方法是很诱人的。用户在M时候只要用cdQFactory的类名)既可以创建出需要的Product?a target="_blank">wayfarer 的文章最后给出的DotNet的实现部分就是这L例子。吕震宇的另一?a >文章 Q关于Simple Factory模式Q的回复?a >Walkdanl出的解x案也是这L。从本质上,q两个例子都不是U的Factory Method模式或者Simple Factory模式Q而是Simple Factory模式和Factory Method模式的组合应用?/p>
引用Walkdan的回复:
Simple Factory最大的好处是把变化集中C一处。另外,LightSimpleFactory.Create()里面的依赖关pM可以消除Q我的做法是:
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中创建对象?/em>
public class LightSimpleFactory.Create(string lightType)
{
ILightCreator creator = creators.Find(lightType);
return creator.Create();
}构造子其实是Factory Method。这? 通过注册构造子Q?.中原来的switch()q程被消除,依赖关系随之解除。其中的Register(), Find()Ҏ(gu)理解Q方法就不写了?/em>
新的cd可通过新注册构造子来实现扩展。当?中的注册代码仅仅是示例,完全可以攑ֈ配置文g中去实现Q比如:
<lightCreators>
<add name="Bulb" type="BulbLightCreator,"/>
<add name="Tube" type="TubeLightCreator,"/>
</lightCreators>
其ILightCreatorl承体系Q是Factory Method模式。LightSimpleFactory和creators的关pWalkdan没有说明Q也可实CؓStrategy模式?/p>
二。引申问?/p>
三。结?/p>
一个纯的Factory Method模式其factory method不可以用static实现Q通过与其他模式结合用可以进行变通。而且通常情况下Factory Method模式都会与其他模式相l合。Abstract Factory模式通常是用Factory Method模式来实现?/p>
1.清晰性和介性是最为重要的Q一个模块的用户永远也不应该被模块的行ؓ所qhQ那样就不清CQ;模块要尽可能的小Q但又不能太〔术语模块(moduleQ在本书中的用法Q是指Q何可重用的Y件组Ӟ从单个方法,到包含多个包的复杂系l都可以是一个模块〕?/p>
2.代码应该被重用,而不是被拯?/p>
3.模块之间的相依性应该尽可能降低到最?/p>
4.错误应该早被检出来,理想情况下是在编译时刅R?/p>
/** * 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; }注意Q在setRoot时可以只讑֮一?br />
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会自动的试图展开节点nodeQ如果isLeafq回为false。试惻IasksAllowsChildrenq回trueQ则一个结点即使没有子l点Q只要设|了allowsChildrenQ对应的treeNodeModel的isLeaf׃q回true。然而当自动展开节点的时候发现ƈ没有子结点,当然׃把图?img height="10" alt="ParentNode.jpg" src="http://www.aygfsteel.com/images/blogjava_net/pragmatic/JTips/ParentNode.jpg" width="15" border="0" />L了。而先把asksAllowsChildren设ؓfalseQ就不会去试囑ֱ开该节点,然后设ؓtrueQ该节点׃昄?img height="10" alt="ParentNode.jpg" src="http://www.aygfsteel.com/images/blogjava_net/pragmatic/JTips/ParentNode.jpg" width="15" border="0" />?/font>
/** * 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);