2012-04-04 68 views
0

我有一個類,其中我已經初始化靜態塊hashmap。通過鑰匙,我已經重新認識了這門課的價值。爲了創建這個類的對象。我使用構造函數類來獲取構造函數並傳遞參數和創建的對象。條件來選擇哪個getConstructor方法

我在hashmap中有兩個類。爲了創建EchoExpression對象,我需要傳遞兩個參數,對於OutExpression類,我只需要傳遞一個參數(String)。

問:

根據由我需要執行獲得其構造和實施的關鍵返回的類,是否有一個參數或兩個參數的構造。

public class ExampleFactory { 

    private static HashMap<String,Class<?>> hmap = new HashMap<String,Class<?>>(); 

    static 
    {     
     hmap.put("echo", EchoExpression.class);   
     hmap.put("Out", OutExpression.class);      
    } 

    public void getExpo(String key,String expression) 
    { 
    Class aClass =map.get(key); 

    //Constructor implementation for OutExpression where only one argument string is passed 

    Constructor constructor = aClass.getConstructor(new Class[]{String.class}); 

    Object object= constructor.newInstance(expression); 

    //constructor for passing two arguments string for EchoExpression 

    Constructor constructor = aClass.getConstructor(new Class[]{String.class,Class.class}); 

    Object object= constructor.newInstance(expression, Boolean.class); 


    return null;   
    }     
} 

如何從值(類)中選擇哪個類實現而不使用if else?

+0

這是什麼語言? – EdChum 2012-04-04 22:43:12

回答

1

使用Enum並打開它。這裏是一個可執行的存根,而沒有深入到你的例子的反射或語法中:

package com.trip.test; 

import java.util.HashMap; 
import java.util.Map; 

public class ExampleFactory { 

    private static Map<String, Class<?>> hmap = new HashMap<String, Class<?>>(); 

    static { 
     hmap.put("echo", EchoExpression.class); 
     hmap.put("Out", OutExpression.class); 
    } 

    public static void getExpo(String key, String expression) { 

     Class aClass = hmap.get(key); 

     ClassMappingEnum myType = ClassMappingEnum.getClassMappingEnum(aClass); 

     switch (myType) { 
     case ECHO_EXPRESSION:{ 
      System.out.println(aClass.getName()); 
      // do something 
      break; 
     } 
     case OUT_EXPRESSION:{ 
      System.out.println(aClass.getName()); 
      // do something 
      break;   
     } 
     case UNKNOWN: 
     default: 
      System.out.println("Bummer: " + aClass.getName());   
     } 

    } 

    public static void main(String[] args) { 
     getExpo("echo", "B"); 
     getExpo("Out", "B"); 
    } 
} 

enum ClassMappingEnum { 
    ECHO_EXPRESSION(EchoExpression.class), OUT_EXPRESSION(OutExpression.class), UNKNOWN(null); 

    private Class typeDes; 

    private ClassMappingEnum(Class typeDes) { 
     this.typeDes = typeDes; 
    } 

    public static ClassMappingEnum getClassMappingEnum(Class compare) { 
     for (ClassMappingEnum cme : ClassMappingEnum.values()) { 
      if (cme.typeDes.equals(compare)) { 
       return cme; 
      } 
     } 
     return UNKNOWN; 
    } 


} 

class EchoExpression<T> { 
    private String someString; 
    private Class<T> someClass; 
    public EchoExpression(String someString, Class<T> someClass) { 
     super(); 
     this.someString = someString; 
     this.someClass = someClass; 
    } 
    public String getSomeString() { 
     return someString; 
    } 
    public void setSomeString(String someString) { 
     this.someString = someString; 
    } 
    public Class<T> getSomeClass() { 
     return someClass; 
    } 
    public void setSomeClass(Class<T> someClass) { 
     this.someClass = someClass; 
    } 


} 

class OutExpression { 
    private String someString; 

    public OutExpression(String someString) { 
     super(); 
     this.someString = someString; 
    } 

    public String getSomeString() { 
     return someString; 
    } 

    public void setSomeString(String someString) { 
     this.someString = someString; 
    } 

} 
0

如果你可以修改類,所以兩者構造具有相同的簽名(接受以相同的順序相同數量/類型的參數),你可以做

Constructor constructor = aClass.getConstructor(new Class[]{String.class,Class.class}); 
Object object= constructor.newInstance(expression, Boolean.class); 

兩個類。

當然,這意味着,目前不需要額外的參數類,則要忽略傳入的它的變化

更新後未使用前一個:這裏是一個可能的方式使用工廠類實現這個想法:

public interface ObjectFactory 
{ 
    Object create(String expr, Class cls); 
} 

public class EchoExpressionFactory implements ObjectFactory 
{ 
    public EchoExpression create(String expr, Class cls) 
    { 
    return new EchoExpression(expr, cls); 
    } 
} 

public class OutExpressionFactory implements ObjectFactory 
{ 
    public OutExpression create(String expr, Class cls) 
    { 
    return new OutExpression(expr); 
    } 
} 

public class ExampleFactory { 

    private static HashMap<String,ObjectFactory> hmap = new HashMap<String,ObjectFactory>(); 

    static 
    {      
     hmap.put("echo", new EchoExpressionFactory());   
     hmap.put("Out", new OutExpressionFactory());       
    } 

    public void getExpo(String key,String expression) 
    { 
    ObjectFactory factory = map.get(key); 

    //Constructor implementation for Expression 
    Object object = factory.create(expression); 

    Object object= constructor.newInstance(expression, Boolean.class); 

    return; 
    }     
} 
+0

我不能修改類,因爲它的jar文件類 – Jessie 2012-04-04 22:53:11

+0

那麼你最好的選擇是用'if'和'instanceof'。如果你真的想避免'if',你可以使用/存儲工廠類來創建你想要的實際對象,並且有一個包含所有必需參數的共同創建方法 - 你基本上會按照我上面的建議做,但是工廠類代替 – Attila 2012-04-04 22:58:33

+0

另外,如何接受[你上一個問題](http://stackoverflow.com/questions/10018565/how-to-access-object-inside-static-block/)的答案之一,因爲你最終使用了什麼建議? – Attila 2012-04-04 23:01:10