Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Programmer > passing a Facto...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 43 Topic 52605 of 55513
Post > Topic >>

passing a Factory to a method to create a generic instance

by thufir <hawat.thufir@[EMAIL PROTECTED] > May 9, 2008 at 11:46 AM

The two nearly identical methods, loadGuests and loadRooms, I would like 
to consolidate by passing in a Factory.  However, I haven't found much 
do***entation on what that looks like.

Guest and Room implement Factory, but that's just part of the picture.

public Guest new() {return new Guest());}    //in Guest.java
public Room new() {return new Room());}      //in Room.java

Is the idea to make a single method, outside of Room and Guest, which 
returns an instance of, well, type <T>?

But, how?  I understand that one approach is to pass a Factory to 
FileUtils.load(), which is fine, but how is that Factory instance first 
created?  Where's it created, in the driver which invokes FileUtils.load
()?

Here's what I have, it's just the top method which I'm working on right 
now:

thufir@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 cat src/a00720398/interfaces/
Factory.java

package a00720398.interfaces;


public interface Factory <T>{
        T make();
}
thufir@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 cat src/a00720398/util/
FileUtil.java
/**
 * FileUtil.java
 */

package a00720398.util;

im****t java.util.*;
im****t java.io.*;
im****t a00720398.data.*;
im****t a00720398.interfaces.*;


public abstract class FileUtil {

        public static <E> List<E> load(File file) {
                List<E> foos = new ArrayList<E>();
                List<String> lines = getLines(file);
                for(String line : lines){
        //              <E> foo = new <E>();  //how do I pass in a 
		//				      //factory to return an "E"?
                }
                return new ArrayList<E>();
        }

        /*
        a00720398/util/FileUtil.java:31: cannot find symbol
        symbol  : class T
        location: class a00720398.util.FileUtil
                public static void factoryTest(Factory<T> factory) {}
                                                       ^
        1 error */
        public static void factoryTest(Factory<T> factory) {} //

/**

interface Factory<T> { T make();}
public <T> Collection<T> select(Factory<T> factory, String statement) {
     Collection<T> result = new ArrayList<T>();
     /* run sql query using jdbc 
     for (/* iterate over jdbc results  ) {
        T item = factory.make();
        /* use reflection and set all of item’s fields from sql results 
        result.add(item);
     }
     return result;
}
You can call this either as
select(new Factory<EmpInfo>(){ public EmpInfo make() {
                                        return new EmpInfo();
                                 }}
    , ”selection string”);


        public static <T> factory<T>(T type) {
                return new List<T>(type);
        }



        @[EMAIL PROTECTED]
 http://www.ibm.com/developerworks/java/library/j-
djc02113.html

        Utilities.make(Integer(0))  //called from driver
        <T extends Object> public static <T> factory (T first) {
                return ew List<T>(first);
        }


class Utilities {
   <T extends Object> public static List<T> make(T first) {
     return new List<T>(first);
   }
}

*/

        //this method is a repaid of loadGuests
        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();
                        System.out.println(rooms);
                }

                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);
                }
                return guests;
        }

        private static List<String> getTokens (String string){
                List<String> tokens = new ArrayList<String>();
                Scanner lineScanner = new Scanner(string);
                while (lineScanner.hasNext()){
                        String token = lineScanner.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);
                                }
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
                return lines;
        }
}
thufir@[EMAIL PROTECTED]
 




thanks,

Thufir
 




 43 Posts in Topic:
passing a Factory to a method to create a generic instance
thufir <hawat.thufir@[  2008-05-09 11:46:52 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-09 14:30:22 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-10 01:19:13 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-10 01:34:29 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-09 23:12:44 
Re: passing a Factory to a method to create a generic instance
Mark Space <markspace@  2008-05-10 01:36:57 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-10 14:37:27 
Re: passing a Factory to a method to create a generic instance
Mark Space <markspace@  2008-05-10 10:33:02 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-10 14:48:22 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-10 18:00:30 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-11 12:59:45 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-11 10:31:54 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-11 20:16:59 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-11 20:46:51 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-11 16:03:32 
Re: passing a Factory to a method to create a generic instance
Mark Space <markspace@  2008-05-11 19:31:20 
Re: passing a Factory to a method to create a generic instance
=?ISO-8859-1?Q?Arne_Vajh=  2008-05-11 22:33:54 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-12 19:33:51 
Re: passing a Factory to a method to create a generic instance
Mark Space <markspace@  2008-05-12 13:11:07 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-12 19:46:02 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-13 11:56:29 
Re: passing a Factory to a method to create a generic instance
Mark Space <markspace@  2008-05-18 21:23:54 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-19 16:51:17 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-19 21:38:47 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-20 12:57:40 
Re: passing a Factory to a method to create a generic instance
=?ISO-8859-1?Q?Arne_Vajh=  2008-05-20 21:55:38 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-21 12:18:06 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-21 07:48:07 
Re: passing a Factory to a method to create a generic instance
=?UTF-8?B?QXJuZSBWYWpow7h  2008-05-21 19:00:43 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-21 23:59:25 
Re: passing a Factory to a method to create a generic instance
=?UTF-8?B?QXJuZSBWYWpow7h  2008-05-31 21:26:24 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-22 18:34:08 
Re: passing a Factory to a method to create a generic instance
=?ISO-8859-1?Q?Arne_Vajh=  2008-05-21 19:11:04 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-21 16:38:45 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-22 19:18:56 
Re: passing a Factory to a method to create a generic instance
=?ISO-8859-1?Q?Arne_Vajh=  2008-05-22 17:50:34 
Re: passing a Factory to a method to create a generic instance
thufir <hawat.thufir@[  2008-05-09 16:23:56 
Re: passing a Factory to a method to create a generic instance
Mark Space <markspace@  2008-05-09 13:17:08 
Re: passing a Factory to a method to create a generic instance
thufir <hawat.thufir@[  2008-05-10 04:48:07 
Re: passing a Factory to a method to create a generic instance
thufir <hawat.thufir@[  2008-05-10 04:53:06 
Re: passing a Factory to a method to create a generic instance
Lew <lew@[EMAIL PROTEC  2008-05-10 08:46:16 
Re: passing a Factory to a method to create a generic instance
Roedy Green <see_websi  2008-05-20 18:07:53 
Re: passing a Factory to a method to create a generic instance
Tom Anderson <twic@[EM  2008-05-21 00:39:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 19:57:00 CST 2008.