malcolmmc wrote:The problem is the OP's approach to cloning. He uses serialization instead of implementing the Cloneable interface. This has two problems. First it is very slow and second it always gives one a deep clone, even for immutable objects which are composite parts.endasil wrote:You're right, but I think my suggestion could still apply. If cloning is taking a lot of your run time then maybe it can be avoided.
That was not the OP's class, but was rather introduced by calvin.
Infiniti wrote:An immutable object is one where the value(s) stored inside will never change after it has been instanciated. Examples are String, Class and all primitive wrappers (Boolean, Short, Integer, ...)
Can any one explain difference between mutable and immutable objects? And how will it reduce the time? Yes ofcourse i need deep clone indeed.
Infiniti wrote:Hope is not a good principle to base ones design on ;-) I don't want to be rude, but having to resort to those measures is [code smell|http://en.wikipedia.org/wiki/Code_smell] and I'd like to find out if there is an alternative route that you can take here.
Its not a design flaw i hope.
Consider this as a scenario there will a search form on submitting this search form i will get search result.So with "search form" you mean the object that represents whatever the user has entered into the form, is that right?
So there is a necessity to clone this search form.Why? I don't get it. Do you still need to keep the unmodified version after you change something? Or is there another reason why you need to clone it.
But there are different categories of search and each category will have different search forms (there will be no parent child relation bet these search forms). So i need to clone these search fomrs for different categories. And also all these search forms will implement serializeble. Please let me know if you need any more information.Can't you simply require each of those search forms to implement Clonable and provide a public clone method? From what you've described you need to be able to clone a precisely defined set of objects and not any arbitrary object, as your previous posts have seemed to imply.
Infiniti wrote:How and why does cloning give you an advantage over simply creating a new "search form"?
3) Because i need to maintain all the pages (simply like browser history) visited by the user as a list and he can navigate to any of those pages at any point of time. Please let me know if you need more explanation.
Infiniti wrote:So what? When I navigate to a new page in my browser, it may cache the old page, but the new page isn't a replica of the old page. It's a new page!
I will store the original object which has data entred by user into cache so that i can provide list of pages he visited as a list of links. He can visit any of the pages he visited at any point of time in the session (exactly like browser histoty but only for current session alone) so that he gets back all the values he entered. (Just maintaining the state that is the business requirement).
better than this?
Page old = browser.getPage(); cache.add(old); Page newPage = old.clone(); browser.setPage(newPage);
(This is all fake code). I'm trying to figure out why you need to clone.
Page old = browser.getPage(); cache.add(old); Page newPage = new Page(browser.getLink()); browser.setPage(newPage);