Como refatorar dois métodos iguais com tipo de retorno diferentes?

4 respostas
developer.schneider

Olá,
Estou precisando de uma ajuda em refatoração, observem esses dois métodos:

private String BuscaValoresRetornadosModSeg(String strXML,
			String strTagPai, String strTagFilha) throws Exception {
		if (strXML != "") {
			Document doc = carregarXmlPorString(strXML);
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName(strTagPai);

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;

					return getValorTag(strTagFilha, eElement);
				}
			}
		}

		return "";
	}
private List<String> BuscarValoresRetornadosModSeg(String strXML,
		String strTagPai, String strTagFilha) throws Exception {
		List<String> lstRetorno = new ArrayList<String>();
		if (strXML != "") {
			Document doc = carregarXmlPorString(strXML);
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName(strTagPai);

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;

					lstRetorno.add(getValorTag(strTagFilha, eElement));
				}
			}
		}

		return lstRetorno;
	}

Não gostaria de deixá-los duplicados assim, como eu poderia refatorar??
Obrigado.

4 Respostas

ErickRAR

Você pode utilizar apenas o da lista. A diferença é que as vezes pegaria uma lista dom apenas um elemento.

F

Eu faria assim:

private String BuscaValoresRetornadosModSeg(String strXML,
			String strTagPai, String strTagFilha) throws Exception {
                return BuscarValoresRetornadosModSeg(strXML, strTagPai, strTagFilha).get(0);
	}
private List<String> BuscarValoresRetornadosModSeg(String strXML,
		String strTagPai, String strTagFilha) throws Exception {
		List<String> lstRetorno = new ArrayList<String>();
		if (strXML != "") {
			Document doc = carregarXmlPorString(strXML);
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName(strTagPai);

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;

					lstRetorno.add(getValorTag(strTagFilha, eElement));
				}
			}
		}

		return lstRetorno;
	}

Resolve?

developer.schneider

Resolve, ficou uma maravilha. Obrigado!

erico_kl
Lembrando que você não está fazendo sobrecarga de métodos neste caso (repare que o nome dos métodos são diferentes: Busca... Buscar). Se você deixar os 2 métodos com a mesma nomenclatura, seu código não vai compilar pois o que interessa para o compilador é a assinatura do método com seus parâmetros, ou seja, você pode ter isso:
private String metodo(int a) {
     ...
}

private String metodo(String a) {
     ...
}
mas não pode ter isso:
private String metodo(int a) {
     ...
}

private int metodo(int a) {
     ...
}
Porque o que interessa para o compilador (para fins de identificação) é somente metodo(int). Temos então duas assinaturas idênticas, portanto não compila.
Criado 23 de outubro de 2012
Ultima resposta 23 de out. de 2012
Respostas 4
Participantes 4