2012-10-22 30 views
0

我們使用AutoBeans創建用於RPC調用的Pojo對象。對於Pojo有一個默認值或其他類初始化的建議方法是什麼?GWT Autobean設置已創建界面的初始值

例如

public interface SamplePojo { 
     // should default to 5 
     int getSampleProperty(); 
     void setSampleProperty(int sampleProperty); 
    } 


    public interface ModelFactory extends AutoBeanFactory { 
     AutoBean<SamplePojo> getSamplePojo(); 
    } 

而且SamplePojo有我們總是希望默認爲5

回答

1

AutoBeans應該被看作是低層次的,映射直/ JSON從一個整型屬性。考慮到這一點,你不想getSampleProperty() 5,你寧願要檢測的情況下的特定值在這種情況下,財產和使用 5。

因此,如果0int的默認值)不是該屬性的可接受值,那麼只需「如果該屬性爲0,則使用5」。否則,請將退貨類型更改爲Integer,如果屬性爲null,則使用5。

+0

這並沒有解決我們希望初始化邏輯集中的問題。在我們的例子中,我們不希望每個使用SamplePojo的地方都必須重複「如果屬性爲null,則使用5」。 – Kenoyer130

+1

然而,AutoBeans只不過是一個類型檢查的'Map';所以無論是明確地把一個5每次創建時間這樣的AutoBean(即使用隱藏AutoBeanFactory並添加初始化邏輯工廠方法),但是,那麼您會明確地通過網絡發送一個5;或以某種方式隱藏AutoBean和/或其'getSampleProperty'(我說了AutoBeans是一個低級別的API?)爲貫徹落實「使用5,如果屬性爲null」的行爲(和優化了線的有效載荷,而'sampleProperty'除非明確設定)。如果你知道protobuf,AutoBean會從中借鑑很多:低級! –

0

這項工作?

public interface SamplePojo { 
     // should default to 5 
     int getSampleProperty(); 
     void setSampleProperty(int sampleProperty); 
    } 

public class SamplePojoImpl implements SamplePojo{ 
    private int sampleProperty = 5 
    // getters/setters 
    int getSampleProperty(){ return sampleProperty;} 
    void setSampleProperty(int sampleProperty){this.sampleProperty = sampleProperty;} 

} 

public interface ModelFactory extends AutoBeanFactory { 
    AutoBean<SamplePojo> getSamplePojo(SamplePojoImpl samplePojo); 
}