Hej.
Mam taki problem mianowicie potrzebuje pewnego mechanizmy do napisani mojego programu.
Napiszę co na razie zrobiłem.
Mam mianowicie takie klasy:
public interface InputPattern {
public ArrayList<Double> getVector();
}
public class InputPatternICPR1 implements InputPattern{
private ArrayList<Double> vector = new ArrayList<Double>();
private ArrayList<String> attributes = new ArrayList<String>();
private String fileName = null;
private String directory = null;
public InputPatternICPR1(String path){
File file = new File(path);
//adding filename to input pattern
this.fileName = file.getName();
//in this case we need directory to further proper images displaying
this.directory = file.getParent();
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s;
//adding atributes name to input pattern
s = br.readLine();
String[] tempAtributes = s.split(";");
this.attributes.addAll(Arrays.asList(tempAtributes));
//geting weight vector
while((s = br.readLine()) != null){
String[] vectorText = s.split(";");
for(int i=0; i<vectorText.length; i++){
this.vector.add(Double.parseDouble(vectorText[i]));
}
}
fr.close();
} catch (FileNotFoundException e) {
System.out.println("Such file does not exist.");
} catch (IOException e) {
System.out.println("Could not read the file.");
}
}
@Override
public ArrayList<Double> getVector() {
return vector;
}
public ArrayList<String> getAttributes() {
return attributes;
}
public String getDirectory() {
return directory;
}
public String getFileName() {
return fileName;
}
}
public interface DataLoader {
public ArrayList<InputPattern> getData();
}
public class DataLoaderICPR1 implements DataLoader{
private ArrayList<InputPattern> data = new ArrayList<InputPattern>();
public DataLoaderICPR1 (String path){
Collection<File> allPaths = new ArrayList<File>();
addFilesRecursively(new File(path), allPaths);
Iterator<File> iter = allPaths.iterator();
while(iter.hasNext()){
File file = (File) iter.next();
if(file.isFile()){
try {
String pathToFile = file.getCanonicalPath();
InputPattern inputPattern = new InputPatternICPR1(pathToFile);
this.data.add(inputPattern);
} catch (IOException ex) {
Logger.getLogger(DataLoaderICPR1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
@Override
public ArrayList<InputPattern> getData() {
return data;
}
private static void addFilesRecursively(File file, Collection<File> all) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
all.add(child);
addFilesRecursively(child, all);
}
}
}
Opisowo słownie o co chodzi. Mamy Interfejs to danych i klasę to odczytywania tych danych. W tej klasie do odczytywanie metoda getData() zwraca nam ArrayList<InputPattern>. Natomiast samo przypisanie obiektów do tej listy wygląda w następujący sposób:
InputPattern test = new InputPatternICPR(argument);
this.lista.add(test);
Jeszcze na dodatek w InputPattern mamy metodę getVector, która zwraca ArrayList<Double>.
No i do puenty. Mam jeszcze jedną klasę, która w konstruktorze jako argument przyjmuje DataLoader. I tam się dokonują pewne obliczenia (sama data nie jest zmieniana). Po prostu na podstawie danych z getVector obiekt InputPattern jest przypisywany do obiektów w przestrzeni (kolejna ArrayLista ale już inny obiekt). Po tych wszystkich obliczeniach itd. potrzebuje się dostać do pewnych metod specyficznych dla InputPatternICPR. a nie InputPattern. Mogę to robić jawnie rzutując, np,:
ArrayList<InputPattern> test = dane.getData();
InputPatternICPR1 test2 = (InputPatternICPR1)test.get(0);
Tylko czy to jest dobra rozwiązanie?? Czy istnieje jakiś inny mechanizm.
P.S. sorki za długi post ale nie chciałem jak najdokładniej to wymyślić - program w starej wersji ma 3k linii kodu, a chcę przepisać od nowa i dodać więcej funkcjonaliści.