【发布时间】:2011-06-27 17:06:41
【问题描述】:
在我的例子中,实现者应该能够“更新”一个对象。
//creating an instance (follows the active record pattern)
SameClass myObject = SameClass.find(123,params);
//myObject gets replaced by the output of the web api inside, but it feels like an update for the implementator
myObject.update("ask the web api to paint it black");
但是,在 Class 内部,我还没有弄清楚如何一次替换所有属性。这种方法行不通,但也许还有其他机会解决它:
public void update(String newParams) {
//Can't replace "this" (that call returns an instance of "SameClass")
this = ConnectionFramework.getForObject(SameClass.class,"some url",newParams);
}
“ConnectionFramework”实际上是Spring RestTemplate for Android。未简化的版本是:
public void update(HashMap<String,String> params) {
SameClassResponse response = restTemplate.getForObject(ENDPOINT+"/{id}.json",SameClassResponse.class, params);
this = response.getSameClass();
}
【问题讨论】:
标签: java instance instance-variables