我正在维护一个较旧的Java代码库(jvm 1.4),它似乎使用克隆作为对象实例化的替代方案,我猜测它是一种性能优化.这是一个人为的例子:
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 } }
关于过早优化的常见警告尽管如此,这实际上是一个推荐的成语吗?