Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does cloning provide a performance improvement over constructors/factory methods?

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?

like image 758
sk. Avatar asked Mar 19 '09 17:03

sk.


People also ask

What are the advantages of the method clone ()?

Advantages of Object CloningWe don't need to write repetitive codes. The clone() method will perform the same task while writing far fewer lines of code. Clone() helps to copy an array quickly. Cloning is the most efficient/easiest way for copying objects.

What is the difference between copy constructor and clone?

Copy Constructor vs. However, the copy constructor has some advantages over the clone method: The copy constructor is much easier to implement. We do not need to implement the Cloneable interface and handle CloneNotSupportedException. The clone method returns a general Object reference.

What is the purpose of using clone operation?

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.

Which method is used for clone?

Creating a copy using the clone() method clone() to obtain the cloned object reference. The class must also implement java. lang. Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class's object.


2 Answers

Presumably they wanted a copy. Perhaps they want to pass it to another function, and can't be sure that that function won't change it. It's a way of making sure that the method doStuff() is const with respect to the state of the Foo object it's called on.

like image 123
Lou Franco Avatar answered Sep 30 '22 19:09

Lou Franco


One reason to invoke clone() instead of the copy constructor or a factory method is that none of the other options may be available.

Implementing clone() to perform a shallow object copy (deep copy is more involved) is trivial compared to implementing a copy constructor or a factory method to perform the same operation. To implement clone(), a class need simply implement the Cloneable interface and override method clone() with one that invokes super.clone() which usually invokes Object.clone(). Object.clone() copies every property of the original object into the corresponding property of the duplicate, thus creating a shallow copy.

Though implementing clone() is straightforward, it is still easy to forget to implement Cloneable. Consequently, a potential risk of using clone() to duplicate an object is that if the class of that object does neglect to implement Cloneable and clone() invokes Object.clone() either directly or indirectly, it will throw CloneNotSupportedException.

See this code example and a previous discussion on the poor design of the Cloneable interface.

like image 33
Derek Mahar Avatar answered Sep 30 '22 19:09

Derek Mahar