Java Factory Class Problem (Factory Design Pattern)

Java programming topics
Post Reply
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Java Factory Class Problem (Factory Design Pattern)

Post by Herath » Thu Aug 19, 2010 12:40 pm

I have a little class where it produces classes of type "Message". I found this class initially from a j2SE implementation and I am having doubts while implementing it in j2ME since j2ME lacks java.lang.reflect package.

Here is the Class.

Code: Select all

class MessageFactory {
    private MessageFactory() {
    }

    static Message createInboundMessage(String message, NetworkThread s) throws Exception {
        try {
            StringBuffer className = new StringBuffer("net.msnlib.");
            if (Character.isDigit(message.charAt(0)) ) {
                className.append('E');
            }
            className.append(message.substring(0, 3));
            className.append("Message");
            //Class m = MessageFactory.class.getClassLoader().loadClass(className.toString());
            
            //Constructor con = m.getConstructor(new Class[] {String.class, NetworkThread.class});
            //return (Message)con.newInstance(new Object[] {message,s});
            return (Message)Class.forName(className.toString()).newInstance();
        } catch (Exception e) {
             e.printStackTrace();
             throw new Exception("Un-known command");
        }
        
    }
}
What I expect from this class is to produce an instance of Message which is determined by the message type.
For an example, if className.append(message.substring(0, 3)); appends "USR" to the className, the Factory method should produce a "USRMessage" type instance and return it. Then my application can call the processMessage() (which is an abstract method in Message class) of USRMessage class to process the USRMessage.

I can see that this is not going to work as I expected. Because I do not have the "message" and the relevant NetworkThread instance "s" in my Factory method created class. So how can I achieve this?.

It seems to me that I can use "switch" statements to handle the each type of Message type. But that is going to be a pain since I have about 73 Message types to handle. If this is not going to work I will have to reduce the functionality of the application by reducing the number of different message types it can handle. (This is a mobile app and I am considering it as an option. :) )
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Java Factory Class Problem (Factory Design Pattern)

Post by Neo » Thu Aug 19, 2010 10:46 pm

Honestly out of my knowledge. Didn't try this type of coding ever. However did you check the sample implementations which does the thing? I'm sure you can get an idea from it.
Honestly it is not possible to suggest you anything in a situation like this without trying it here.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Java Factory Class Problem (Factory Design Pattern)

Post by Herath » Thu Aug 19, 2010 11:13 pm

I just rewrote the code with switch statements. :)

Code: Select all


package lib.msn;




class MessageFactory {
    
    private MessageFactory() {


    }

    
    static Message createInboundMessage(String message, NetworkThread s) throws Exception {
        try {
            
            String classType;
            if(Character.isDigit(message.charAt(0)) ) {              
                classType="E";
            }else{
            classType=message.substring(0, 3);
            }
            System.out.println("Message From message factory:: Order recieved for a "+classType);
            String msgT;
            
            msgT=s.getMessageType(classType);
           // GetMessageType(String type) is implemented in NetworkThread class. I implemented it there since it is the 
           // only class that will be instantiated during the whole application life. 
           // I have created a Hashtable, when I input the message type (VER,CVR,USR,XFR,MSG .... etc) , i look up the            //   hash table and get the relevant numerical value so i can use it in switch statement... 
                

            System.out.println(msgT);
            System.out.println("");
            int msgSerialNumber=Integer.parseInt(msgT);

            switch(msgSerialNumber){
                case 1:
                    return new VERMessage(message, s);
                case 2:
                    return new USRMessage(message, s);
                case 3:
                    return new SYNMessage(message,s);
                case 4:
                    return new CVRMessage(message,s);
                default:
                    throw new Exception();
            }
            
        } catch (Exception e) {
             e.printStackTrace();
             throw new Exception("Un-known command");
        }
        
    }

    
}

This is what i wanted to do. This method is less memory consuming anyway. I think this is better since mobile devices has only a little amount of memory. :D
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Java Factory Class Problem (Factory Design Pattern)

Post by Neo » Fri Aug 20, 2010 4:46 pm

Great you manage to got that somehow :)
Post Reply

Return to “Java Programming”