Заменить собственный экземпляр внутри java-объекта

В моем случае разработчик должен иметь возможность «обновить» объект.

//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"); 

Однако внутри Класса I убежище т придумал, как заменить сразу все атрибуты. Такой подход не работает, но, возможно, есть другой шанс решить эту проблему:

    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 для Android . Неупрощенная версия:

    public void update(HashMap params) {
SameClassResponse response = restTemplate.getForObject(ENDPOINT+"/{id}.json",SameClassResponse.class, params);
    this = response.getSameClass();
}

5
задан OneWorld 15 February 2011 в 15:48
поделиться