开发者

Duplicating java instance and storing within first instance, how to copy the values from the second instance back to the first?

开发者_运维知识库public class Example {
    public Example duplicate;

    public void duplicateState() {
        this.example = this.clone();
    }

    public void loadDuplicate() {
        // implementation
    }
}

Looking at the above example, you can see that I need to duplicate the Example instance. This is so that objects that want to modify Example must instead modify the duplicate, allowing the main instance to run routines without critical variables being changed. Periodically, the Example will load the duplicate's values and so that it can perform the routines with updated variables. Performance is critical in this. Does anyone know how to implement the loadDuplicate function in the fastest possible way, or are there any other ways to approach this problem?


Use java's serialization feature, then later deserialize it.

See this article for details


THIS IS NOT AN ANSWER, BUT A COMMENT

if you use clone() and do all assignments yourself (as in below mentioned answer, which is actually correct), from maintenance perspective, beware that if you/some one else adds new field(s) to the Example class (say several months from now) you might need to update your mimic() method accordingly which can be easily missed. So unless you notice real performance issues (which am sure you won't), better to stick with inbuilt serialization.. just my 2 cents.. (i know you are ok with just shallow copy, still..)

p.s. couldnt comment as am a new user of SO


If you really need the fastest performance, then both duplicateState() and loadDuplicate() should consist of a sequence of assignments of the fields of Example.

Both cloning and serialization will be significantly slower, though "how much" will depend on the number of fields and their types.

The other question is whether you need to do a deep or shallow copy of the state of an Example. Clone (using the default clone() method will give you a shallow copy, and serialization will give you a (recursively) deep copy.

(FWIW - the last point is a reason why serialization will be slower than cloning. Another is that serialization also needs to include metadata in the serial form ... and that's more data to be copied.)


First of all, I don't think that this will be any faster or better than serializing into a byte array (or some other in-memory structure) and de-serializing, but if you insist on "saving" the state with clone, just implement a method that does the opposite of your clone() method:

public class Example {
    public Example example;
    private Content content;
    //... more fields

    public void saveState() {
        this.example = this.clone();
    }

    public void loadState() {
        mimic( example );
    }

    public Example clone() {
        Example clone = new Example();
        clone.content = this.content;
        clone.example = this.example;
        //... you're doing this with all your fields I presume
        return clone;
    }

    public void mimic( Example model ) {
        this.content = model.content;
        this.example = model.example;
        //... and so on.
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜