动态返回模型类对象

时间:2013-12-26 15:12:20

标签: java spring-mvc

这可能是一个简单而基本的问题,但我在这里苦苦挣扎。我想基于某些动作返回模型类对象。例如;

 Employee emp = Library.getModelObject("employee"); //LibraryManagementController.java

    public static Object getModelObject(String action){   // Library.java

       if(action.equals("employee")){
         Employee emp = new Employee();
         return emp;
       }else if(action.equals("student")){
         Student student = new Student();
         return student;
       }else{
         return null;
       }

    }

1 个答案:

答案 0 :(得分:0)

这听起来像工厂策略模式的好例子。定义工厂接口,然后实现它以创建正确的类型。您还应该考虑使用泛型来处理不同的类型。