2014-10-16 38 views
1

我在java中創建對象時出現問題,我有3個罐子,每個人都有一個名爲「Person」的類,我將這些罐子文件 包含到我的項目中,需要定義3個對象的人,問題是:刪除重複的代碼,調用不同罐子中的相同對象

public class UtilClass { 
    public static com.jar1.Person definePerson1() { 
     com.jar1.Person person = new com.jar1.Person(); 
     person.setName(Constant.NAME); 
     person.setLastName(Constant.LASTNAME); 
     return person; 
    } 

    public static com.jar2.Person definePerson2() { 
     com.jar2.Person person = new com.jar2.Person(); 
     person.setName(Constant.NAME); 
     person.setLastName(Constant.LASTNAME); 
     return person; 
    } 

    public static com.jar3.Person definePerson3() { 
     com.jar3.Person person = new com.jar3.Person(); 
     person.setName(Constant.NAME); 
     person.setLastName(Constant.LASTNAME); 
     return person; 
    } 
} 

正如你所看到的,類是「相同」,但包裝不同的是,我有這個UtilClass因爲我在另一個類中定義的方法:

public void create() { 
    com.jar1.Group = new Group(UtilClass.definePerson1()); //Only accept com.jar1.Person 
    com.jar2.Group = new Group(UtilClass.definePerson2()); //Only accept com.jar2.Person 
    com.jar3.Group = new Group(UtilClass.definePerson3()); //Only accept com.jar3.Person 
} 

我怎樣才能簡化課堂UtilClass並避免重複的代碼?我無法更改我的jar文件。

+0

請問您個人類共享公共接口? – SMA 2014-10-16 13:26:57

+0

醜,但你可以使用反射 – 2014-10-16 13:27:28

+0

謝謝Holger。這只是一個例子。真的,3個jar文件對應3個WS客戶端,每個RequestType都有一個名爲Person的屬性(每個Person類對每個jar文件都是一樣的),我需要用相同的值來設置它。感謝的人, – Candres 2014-10-16 13:39:21

回答

1

您可以使用反射來設置您的值。

例如,使用BeanUtilsConstructorUtils,這更容易比Java bean的API來使用(見answer

public static class UtilClass { 
    public static com.jar1.Person definePerson1() { 
     return newPerson(com.jar1.Person.class); 
    } 

    public static com.jar2.Person definePerson2() { 
     return newPerson(com.jar2.Person.class); 
    } 

    public static com.jar3.Person definePerson3() { 
     return newPerson(com.jar3.Person.class); 
    } 

    public static <T> T newPerson(Class<T> clazz) { 
     try { 
      T person = ConstructorUtils.invokeConstructor(clazz, null); 
      BeanUtils.setProperty(person, "name", Constant.NAME); 
      BeanUtils.setProperty(person, "lastName", Constant.LASTNAME); 
      return person; 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
0

如果三個Persos類沒有用的名字和lastName字段或通用超與setName和setLastName方法的接口,最簡單的解決方案是使用反射。

但是,使用反射是不好的做法。如果稍後將人名重命名爲firstName,則代碼將編譯,並且沒有任何內容會警告您,您的UtilClass已損壞。

1

如果這些類沒有任何共同之處,即沒有實現,你可以使用, 您可以使用標準java.beans包解決的任務,沒有任何第三方庫的通用接口:

import java.beans.Expression; 
import java.beans.Statement; 

public class UtilClass { 
    public static <T> T definePerson(Class<T> type) { 
    try { 
     Object o=new Expression(type, "new", null).getValue(); 
     new Statement(o, "setName", new Object[]{Constant.NAME}).execute(); 
     new Statement(o, "setLastName", new Object[]{Constant.LASTNAME}).execute(); 
     return type.cast(o); 
    } catch(Exception ex) { throw new IllegalStateException(ex); } 
    } 
} 

感謝泛型中,方法聲明返回您傳遞的Class實例的類型。然後你的使用情況會是什麼樣子:

com.jar1.Group = new Group(UtilClass.definePerson(com.jar1.Person.class)); 
com.jar2.Group = new Group(UtilClass.definePerson(com.jar2.Person.class)); 
com.jar3.Group = new Group(UtilClass.definePerson(com.jar3.Person.class)); 
+0

,它的作品完美! – Candres 2014-10-17 13:01:33

相關問題