9

Is cloning good practice in this case? How to do it better?

public ModelCollection startParsing() { return parseFeed(new ModelSpecialEntry); } public ModelCollection parseFeed(ModelEntry pattern) { ModelCollection modelCollection = new ModelCollection(); while( condition ) { //TODO: Is cloning the best solution? ModelEntry model = (ModelEntry) pattern.clone(); model.parse(); //add this item to an collection modelCollection.add(model); } return modelCollection; } 
4
  • ' in this case?' can u elaborate it Commented Sep 14, 2010 at 9:36
  • Tell us more why you need to clone this object in the first place. Commented Sep 14, 2010 at 9:42
  • Because I want to wrap up a collection containing of models. Commented Sep 14, 2010 at 10:49
  • Related question: stackoverflow.com/questions/1106102/… Commented Sep 14, 2010 at 12:30

4 Answers 4

10

Cloning is rarely a good idea in Java. Try other techniques such as Copy constructors or Factory methods.

Wikipedia has a nice article on why clone() has many disadvantages in Java.

Using copy constructors, create a constructor that takes an instance of the current class as parameter, and copy all fields in the local class:

public class Foo { private String bar; private String baz; public Foo(Foo other) { this.bar = other.bar; this.baz = other.baz; } } 

Using factory methods, create a method that takes your object as parameter and return an object containing the same values:

public Foo copyFoo(Foo other) { Foo foo = new Foo(); foo.setBar(other.getBar()); foo.setBaz(other.getBaz()); } 
Sign up to request clarification or add additional context in comments.

3 Comments

You can also have partial copy methods: public withBar(String newBar) { return new Foo(newBar, this.baz); }
You mean static factory method and not factory method, which might be confused with the factory method pattern
So should one avoid prototype pattern ?
2

You could use a copy constructor instead of implementing Cloneable, but it looks like you have a hierarchy of ModelEntry classes, so using clone may be the best approach. See this question for some pointers on whats wrong with Cloneable

Comments

1

I thing, that it is like with everything else in programming: it depends on the object specyfication.

Try to make a really quick test: clone 100000 objects and instantiates the same amount of objects and check time how long it takes (System.currentTimeInMilis()). Often clone is faster.

And remember that with clone there is one problem - when adding new field etc. you need to modify clone() method too.

2 Comments

Oh, modify the clone method? Is this necessary? I thought this is just a method acting kind like a constructor, which is also not necessary.
If you don't implement clone in your classes you'll get a CloneNotSupportedException thrown when you call it.
0

Clone is not a good idea as many programmers agree.

It's error-prone. You have to override clone() carefully.Forgetting invoking super.clone() is a popular bug.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.