我有一个用Java编写的类,我想知道它是正常大小还是大而且应该分成几部分:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class AddressTree
{
private AddressNode structureRoot, userRoot;
private String xmlAddress;
public AddressTree(String XMLaddress)
{
this.xmlAddress = XMLaddress;
createEmptyTree();
startParsing();
}
private void startParsing()
{
AddressXMLParser handler = new AddressXMLParser(this);
try
{
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
File xml = new File(xmlAddress);
parser.parse(xml, handler);
ArrayList<AddressNode> rawNodes = createRawNodes(handler.getAddressStructure());
createTreeFromRawNodes(rawNodes, handler.getStartItem());
for (int i = 0; i < handler.getFinalItems().size(); i++)
{
getAddressNode(handler.getFinalItems().get(i), structureRoot).setIsFinal(true);
}
}
catch (StartAddressNodeNotFoundException ex)
{
Logger.getLogger(AddressTree.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ParserConfigurationException ex)
{
}
catch (SAXException ex)
{
}
catch (IOException ex)
{
}
}
private void createEmptyTree()
{
structureRoot = new AddressNode("root", null);
userRoot = new AddressNode("root", null);
}
public void setValue(String nodeName, String value) throws AddressNodeNotFoundException
{
AddressNode node = getAddressNode(nodeName, userRoot);
if (node != null)
{
node.setNodeValue(value);
}
else
{
throw new AddressNodeNotFoundException();
}
}
public ArrayList<String> getCompleteAddress()
{
return null;
}
public boolean isAddressCompleted()
{
AddressNode iterator = userRoot;
while (iterator.getAllChilds().size() != 0)
{
iterator = iterator.getAllChilds().get(0);
}
if (getAddressNode(iterator.getNodeName(), structureRoot).isFinal())
{
return true;
}
else
{
return false;
}
}
public List<String> getSubAddressItems(String addressItem)
{
ArrayList<String> subAddressItems = new ArrayList<String>();
List<AddressNode> childNodes = getAddressNode(addressItem, structureRoot).getAllChilds();
for (int i = 0; i < childNodes.size(); i++)
{
subAddressItems.add(childNodes.get(i).getNodeName());
}
return subAddressItems;
}
private AddressNode getAddressNode(String nodeName, AddressNode root)
{
ArrayList<AddressNode> graphNodes = new ArrayList<AddressNode>();
ArrayList<AddressNode> passedNodes = new ArrayList<AddressNode>();
graphNodes.add(root);
while (graphNodes.size() != 0)
{
AddressNode cureNode = graphNodes.remove(0);
passedNodes.add(cureNode);
if (cureNode.getNodeName().equals(nodeName))
{
return cureNode;
}
else
{
for (int i = 0; i < cureNode.getAllChilds().size(); i++)
{
if (!passedNodes.contains(cureNode.getAllChilds().get(i)))
{
graphNodes.add(cureNode.getAllChilds().get(i));
}
}
}
}
return null;
}
private ArrayList<AddressNode> createRawNodes(TreeMap<String, ArrayList<String>> addressStructure)
{
int itemsSize = addressStructure.size();
ArrayList<AddressNode> tmpNodes = new ArrayList<AddressNode>();
ArrayList<AddressNode> finalNodes = new ArrayList<AddressNode>();
for (int i = 0; i < itemsSize; i++)
{
String curKey = addressStructure.firstKey();
ArrayList<String> curSubItems = addressStructure.remove(curKey);
AddressNode curNode = new AddressNode(curKey);
finalNodes.add(new AddressNode(curKey));
for (int j = 0; j < curSubItems.size(); j++)
{
curNode.addChlid(curSubItems.get(j));
}
tmpNodes.add(curNode);
}
for (int i = 0; i < finalNodes.size(); i++)
{
for (int j = 0; j < tmpNodes.get(i).getAllChilds().size(); j++)
{
AddressNode childNode = null;
boolean existNodeBefore = false;
for (int k = 0; k < finalNodes.size(); k++)
{
if (finalNodes.get(k).getNodeName().equals(tmpNodes.get(i).getAllChilds().get(j).getNodeName()))
{
childNode = finalNodes.get(k);
existNodeBefore = true;
}
}
if (!existNodeBefore)
{
finalNodes.get(i).addChlid(tmpNodes.get(i).getAllChilds().get(j).getNodeName());
}
else
{
finalNodes.get(i).addChildNode(childNode);
}
}
}
return finalNodes;
}
private void createTreeFromRawNodes(ArrayList<AddressNode> rawNodes, String rootName) throws StartAddressNodeNotFoundException
{
if (rootName == null)
{
throw new StartAddressNodeNotFoundException();
}
for (int i = 0; i < rawNodes.size(); i++)
{
if (rawNodes.get(i).getNodeName().equals(rootName))
{
structureRoot.addChildNode(rawNodes.get(i));
rawNodes.get(i).setParent(structureRoot);
}
}
}
public void addAddressItem(String itemName) throws AddressStructureAbusingException
{
AddressNode iterator = userRoot;
while (iterator.getAllChilds().size() > 0)
{
iterator = iterator.getAllChilds().get(0);
}
AddressNode fatherNode = getAddressNode(iterator.getNodeName(), structureRoot);
for (int i = 0; i < fatherNode.getAllChilds().size(); i++)
{
if (fatherNode.getAllChilds().get(i).getNodeName().equals(itemName))
{
iterator.addChlid(itemName);
return;
}
}
throw new AddressStructureAbusingException();
}
public void removeLastAddressItem() throws AddressNodeNotFoundException
{
if (userRoot.getAllChilds().size() > 0)
{
AddressNode iterator = userRoot;
while (iterator.getAllChilds().get(0).getAllChilds().size() > 0)
{
iterator = iterator.getAllChilds().get(0);
}
iterator.clearChilds();
}
else
{
throw new AddressNodeNotFoundException();
}
}
}
如果必须打破怎么办?
答案 0 :(得分:2)
我当然看到比这更大的课程。
如果您发现难以阅读,我建议 强烈建议在那里添加一些评论。
只要你的课程围绕一个特定的目的,就应该尽可能地达到这个目的。
答案 1 :(得分:2)
我有几个责任,我可以看到:
AddressNode
)至少我将这两个责任分开至少两个不同的类别。
<强> NB 强>
不关心这个问题还有几个空的catch块,see here why they are a bad idea
答案 2 :(得分:2)
你的班级有230行 - 这绝不是过分的。然而,单独的规模只是一个非常模糊的指标,表明是否应该分类。
更通用的指标是cohesion - 基本上,类的大多数方法应该使用其大多数字段。但这也不是你班上的问题 - 它看起来还不错。
答案 3 :(得分:0)
您可能想要适应
if (foo) {
bar();
} else {
fum();
}
代替。这允许代码的长度更短,允许您在屏幕上一次查看更多内容。
另外。永远不要有空的挡块。考虑“在其中抛出新的RuntimeException(”unexpected“,e)”。