为了进一步说明,我想要的是基于客户端服务器架构中的某些参数来识别传入的请求。假设我在服务器上收到一个请求,该请求具有一些参数,例如command=xyzparam1=blahparam2=blah2

some.property.xyz=com.domain.BusinessFunction1
some.property.abc=com.domain.BusinessFunction2

现在,在识别了这些参数并查看了配置之后,我应该能够调用正确的业务功能,如上例所示,如果我收到的命令参数为xyz的请求,则应该通过BusinessFunction1进行,否则,是abc,应通过BusinessFunction2。并且BusinessFunction1BusinessFunction2在两个不同的Java类中实现。

最佳答案

最好的方法是创建一个单例工厂,可以根据服务名称com.domain.BusinessFunction1从存储的数据库some.property.xyz中填充该工厂。工厂将使用Class.forName("com.domain.BusinessFunction1");使用反射来构造类,然后您可以在此类上调用一个方法,该方法为了便于使用而需要映射属性名和值。我个人将创建另一个工厂,并为每个业务功能具有特定的对象。如果没有数据库,则可以将业务功能的名称值存储在XML或枚举中。

这是使用Singleton和HashMap的想法。我建议您从数据库填充工厂,以提高灵活性。

通用基本接口

public interface IBusinessFunction {
    public void doFunction();
    public void setParameters(Map<String,String> paramMap);
}


业务职能

public class BusinessFunction1 implements IBusinessFunction {
    @Override
    public void doFunction() {
        System.out.println(String.format("Function from business class %s called.", this.getClass().getName()));
    }

    @Override
    public void setParameters(Map<String, String> paramMap) {

    }
}


public class BusinessFunction2 implements IBusinessFunction {
    @Override
    public void doFunction() {
        System.out.println(String.format("Function from business class %s called.", this.getClass().getName()));
    }

    @Override
    public void setParameters(Map<String, String> paramMap) {

    }
}


商业工厂

public class BusinessFactory {
    private static BusinessFactory instance = null;
    private Map<String, Class<? extends IBusinessFunction>> businessFunctionMap = null;

    protected BusinessFactory() {
        // Populate this from a database,
        // for the ease of your example i will use a HashMap
        businessFunctionMap = new HashMap<String, Class<? extends IBusinessFunction>>();
        businessFunctionMap.put("some.property.xyz", BusinessFunction1.class);
        businessFunctionMap.put("some.property.abc", BusinessFunction2.class);
    }

    public static BusinessFactory getInstance() {
        if (instance == null) {
            instance = new BusinessFactory();
        }
        return instance;
    }

    public IBusinessFunction getBusinessFunction(String property) {
        IBusinessFunction businessFunction = null;
        Class clazz = businessFunctionMap.get(property);
        try {
            Constructor constructor = clazz.getConstructor();
            businessFunction = (IBusinessFunction) constructor.newInstance();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
      return businessFunction;
    }
}


测试你的班级和工厂

public class Example {
    public static void main(String[] args) {
        Map<String,String> parameters = new HashMap<String, String>();
        parameters.put("Param1","param 1 value");
        parameters.put("Param2","param 2 value");

        IBusinessFunction businessFunction = BusinessFactory.getInstance().getBusinessFunction("some.property.abc");
        businessFunction.setParameters(parameters);
        businessFunction.doFunction();
    }
}

10-02 21:37