I'm maintaing an older Java code base (jvm 1.4) that seems to use cloning as an alternative to object instantiation, I'm guessing as a performance optimization. Here's a contrived example:
public class Foo {
private SomeObject obj; // SomeObject implements Cloneable
public Foo() {
obj = new SomeObject();
obj.setField1("abc"); // these fields will have the same value every time
obj.setField2("def");
}
public void doStuff() {
SomeObject newObj = obj.clone(); // clone it instead of using a factory method
// do stuff with newObj
}
}
The usual caveats about premature optimization notwithstanding, was this actually a recommended idiom at some point?