Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Universal factory method

  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Universal factory method

    Hello, I am trying to create an univerasl factory method that can return everything, from Strings to JFrames. Here is what I was concepting:

    Java Code:
    public interface Factory {
    
    }
    Java Code:
    public  class FactoryMethod {
       public static Factory make(Factory f) {
                 return f;
        }
       
       public static void main(String[] args) {
          FactoryMethod.make(new Factory() {
                  
                 public String getString() {
                     String abc = "ABC";
                     return abc;
                }
                // cant call getString() in new Factory()
         }); //cant call .getString() after make ) too
    }
    The question is how I call the getString() method? I`ve tried various ways but it does not work to me. Any idea how to improve it or how to call it?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Universal factory method

    The question is how I call the getString() method?
    With respect, Java is a strongly typed language. And, in the context of Java, the question is "Why would I call the getString() method?"

    By strongly typed I mean that if you want to say foo.getString() then the compiler will insist that foo as declared as a type (class or interface) with a getString() method declared. Other languages don't work this way: for instance in JavaScript you can always call foo.getString() and it will be fine if a getString() function happens to be defined for whatever it is that foo references at runtime.

    ---

    I think you can call getString() within the anonymous class definition you supply to the new Factory() constructor. If you get a compiler message post it, and the code.

    In general, however, you can't call getString() on the Factory instance created by new Factory(). That's because new Factory() gives you a reference to a Factory, and a Factory is not declared to have a getString() method.
    heatblazer likes this.

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Universal factory method

    Thanks, I came with a solution ( ironically after some coffeee beans now :) ) Take a look a that:
    Factory.java
    Java Code:
    public interface Factory {
    
    	Object getStr(Object o);
    
    }
    and the ComponentFactory:
    Java Code:
    public class ComponentFactory {
    
    	public abstract static class MK implements Factory {
    		public abstract Object getStr(Object o);
    	}
    	public static MK make(MK inst) {
    				return inst;
    			}
    	
    	public static void main(String[] args) {
    	
    		String s = (String)(ComponentFactory.make(new MK() {
    		
    			public Object getStr(Object o) {
    				String abc = "abc";
    				System.out.println("Called");
    				return (String) abc;
    				}
    			})).getStr(null);
    		System.out.println("getStr() returned "+s);	
    	}
    
    }
    When I compile and run:
    [ilian@ilian ilian]$ java ComponentFactory
    Called
    getStr() returned abc

    It wokrs but casts are 100% needed otherwise I guess it refers to MK. ( Sorry for the unconvinient naming, I was just experimenting with what you proposed). Kind regards for making a clarity!

Similar Threads

  1. Universal Password Manager 1.11
    By java software in forum Java Software
    Replies: 0
    Last Post: 04-13-2013, 03:29 AM
  2. Universal Password Manager (UPM) 1.8
    By java software in forum Java Software
    Replies: 0
    Last Post: 01-31-2012, 06:18 PM
  3. Instantiation using an instance factory method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 01:35 PM
  4. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  5. Instantiation using an instance factory method
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •