Re: passing a Factory to a method to create a generic instance
On Fri, 09 May 2008 14:30:22 +0100, Tom Anderson wrote:
I've had a look through your code, and it all seems a bit complicated. I
notice that you're planning to use reflection to move data from JDBC
result sets to your objects. That's kind of gross. But if you are going
to do that, you don't need a factory: just pass in the Class object for
the objects you want to create (Guest.class or Room.class), then use the
newInstance method on the class object to create objects to feed to your
reflective loader. You can do this for loading from a file too. No
factories, just reflection. Although really, the Class object is acting
as a factory.
I'd prefer not to use reflection, is that possible? I don't want to make
it complicated.
Rather than a RoomFactory class which implements Factory, could Room
implement Factory?
for:
public List load(Factory fac) {
List l = new ArrayList() ;
for (String[] row : somehowReadDataFromSomewhere()) {
Object obj = fac.make(row) ; //this seems weird
l.add(obj) ;
}
return l ;
}
Can I do something better than creating Object obj ? Somehow use
generics, perhaps a wildcard ? operator?
Thank you for the information! I'm not sure that I'm going to do this,
no points off for what I have now, but it just burns me up to have nearly
identical methods:
loadGuest
loadRoom
loadFoo
which are nearly indistinguishable.
More recent code, cleaned up a bit from what you probably saw:
thufir@arrakis:~/bcit-comp2611-project1$
thufir@arrakis:~/bcit-comp2611-project1$ cat src/a00720398/util/
FileUtil.java
/**
* FileUtil.java
*/
package a00720398.util;
import java.util.*;
import java.io.*;
import a00720398.data.*;
import a00720398.interfaces.*;
public abstract class FileUtil {
public static void outputRooms(List<Room> rooms, File file){
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(file);
for (Room room : rooms) {
outputStream.println(room);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStream.close();
}
}
public static void outputGuests(List<Guest> guests, File file){
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(file);
for (Guest guest : guests) {
outputStream.println(guest);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStream.close();
}
}
public static void outputRooms(List<Room> data){}
public static List<Room> loadRooms(File file) {
List<Room> rooms = new ArrayList<Room>();
List<String> lines = getLines(file);
for (String line : lines) {
List<String> tokens = getTokens(line);
Room room = new Room(tokens);
rooms.add(room);
}
return rooms;
}
public static List<Guest> loadGuests(File file) {
List<Guest> guests = new ArrayList<Guest>();
List<String> lines = getLines(file);
for (String line : lines) {
List<String> tokens = getTokens(line);
Guest guest = new Guest(tokens);
// System.out.println(guest);
guests.add(guest);
}
// System.out.println(guests);
return guests;
}
private static List<String> getTokens (String string){
List<String> tokens = new ArrayList<String>();
Scanner tokenScanner = new Scanner(string);
tokenScanner.useDelimiter("\t");
while (tokenScanner.hasNext()){
String token = tokenScanner.next();
tokens.add(token);
}
return tokens;
}
private static Guest newGuest(String string) {
List<String> guestData = getTokens(string);
Guest guest = new Guest(guestData);
return guest;
}
private static List<String> getLines(File file) {
List<String> lines = new ArrayList<String>();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\n");
while (lineScanner.hasNextLine()) {
String oneLine = lineScanner.next
();
lines.add(oneLine);
// System.out.println("*****\n\n\n"
+ oneLine);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return lines;
}
}
thufir@arrakis:~/bcit-comp2611-project1$
thufir@arrakis:~/bcit-comp2611-project1$
thanks,
Thufir //getting some zzz's