Created
October 6, 2016 22:43
-
-
Save Marlysson/70c3b1b5a96637230e44ca33948b88b7 to your computer and use it in GitHub Desktop.
Implementação do FactoryMethod em Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface LeitorDeArquivos{ | |
public List<String> ler(File arquivo); | |
} | |
class LeitorTXT implements LeitorDeArquivos{ | |
public ArrayList<String> ler(File arquivo){ | |
//Processamento java para retornar as linhas do arquivo de texto. | |
//Separando cada linha pelo final do arquivo. | |
//Retornando uma lista de palavras. | |
} | |
} | |
class LeitorCSV implements LeitorDeArquivos{ | |
public ArrayList<String> ler(File arquivo){ | |
//Processamento java para ler as linhas do arquivo csv. | |
//Separando o cabeçalho e lendo cada linha pelo seu separador. | |
//Retornando uma lista de palavras; | |
} | |
} | |
class FactoryLeitorArquivo{ | |
public static criar(String tipoArquivo){ | |
if (tipoArquivo.equals("txt")){ | |
return new LeitorTxt(); | |
}else if (tipoArquivo.equals("csv")){ | |
return new LeitorCSV(); | |
} | |
} | |
} | |
class TokenizerWords{ | |
public List<String> tokenize(File arquivo){ | |
String extensao = arquivo.getExtension(); | |
String nome = arquivo.getNome() | |
LeitorArquivos leitor = FactoryLeitorArquivo.criar(extensao); | |
List<String> palavras = leitor.ler(arquivo); | |
return palavras; | |
} | |
} | |
//Uso | |
TokenizeWords tokenizer = new TokenizerWords() | |
List<String> palavras = tokenizer.tokenize("palavras.txt"); | |
// ["Casa", "Pássaro", "Bahia" , "Programação"] | |
TokenizeWords tokenizer = new TokenizerWords() | |
List<String> palavras = tokenizer.tokenize("palavras.csv"); | |
// ["Casa", "Pássaro", "Bahia" , "Programação"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment