条件选择哪个getConstructor方法

时间:2012-04-04 22:39:21

标签: java choice

我有一个类,我在静态块中初始化了hashmap。通过密钥,我已经重新获得了一个类的值。为了为这个类创建对象。我使用了构造函数类来获取构造函数并传递参数并创建了对象。

我在hashmap中有两个类。要为EchoExpression创建object,我需要传递两个参数,对于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的情况下从值(类)中选择要实现哪个类?

2 个答案:

答案 0 :(得分:1)

使用枚举并打开它。这是一个可执行的存根,没有深入了解您的示例的反射或语法:

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;
    }

}

答案 1 :(得分:0)

如果你可以修改类,那么两个构造函数都有相同的签名(接受相同顺序的相同数量/类型的参数),你可以做

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

两个班级。

这当然意味着现在不需要额外参数的类将不得不忽略更改后未使用的传入

更新:以下是使用Factory类实现该想法的可能方法:

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;
  }                 
}