This discussion is archived
1 2 Previous Next 28 Replies Latest reply: May 26, 2008 11:52 AM by 807591 Go to original post RSS
  • 15. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    Then consider making your Age class immutable. It looks like a suitable candidate. Then there will certainly be no reason to clone it.
  • 16. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    malcolmmc wrote:
    Then consider making your Age class immutable. It looks like a suitable candidate. Then there will certainly be no reason to clone it.
    That was not the OP's class, but was rather introduced by calvin.
  • 17. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    endasil wrote:
    That was not the OP's class, but was rather introduced by calvin.
    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.
  • 18. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    malcolmmc wrote:
    endasil wrote:
    That was not the OP's class, but was rather introduced by calvin.
    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.
    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.
  • 19. Re: is this write way to clone a object?
    800578 Newbie
    Currently Being Moderated
    Can any one explain difference between mutable and immutable objects? And how will it reduce the time? Yes ofcourse i need deep clone indeed.

    Edited by: Infiniti on May 27, 2008 12:16 AM
  • 20. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    an example of the "right way" is given here:

    [http://www.java-tips.org/java-se-tips/java.lang/how-to-implement-cloneable-interface.html]
  • 21. Re: is this write way to clone a object?
    JoachimSauer Journeyer
    Currently Being Moderated
    Infiniti wrote:
    Can any one explain difference between mutable and immutable objects? And how will it reduce the time? Yes ofcourse i need deep clone indeed.
    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, ...)

    They have several advantages. One of them is that you never have to clone them, you can simply use them in multiple places at once, because noone can (accidentally or deliberately) change them.

    An example for a mutable object would be an ArrayList. You can't easily use it in multiple places, because at one place you might add a new element, which might not be expected in another place.

    But you still didn't tell us why you need to be able to clone arbitrary objects (even those that where not designed to be clonable). It smells like a bad design, if you need to do this. Could you tell us why that is so?
  • 22. Re: is this write way to clone a object?
    800578 Newbie
    Currently Being Moderated
    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 there is a necessity to clone this search form. 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.
  • 23. Re: is this write way to clone a object?
    JoachimSauer Journeyer
    Currently Being Moderated
    Infiniti wrote:
    Its not a design flaw i hope.
    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.
    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.
  • 24. Re: is this write way to clone a object?
    800578 Newbie
    Currently Being Moderated
    1) Let me again for the design. Thanks.
    2) Yes you are right.
    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.
    4) Yes i can do that in what ever way you have said. And also it is a design change then. But a point to be noted is "there is a possibility of adding more categories in the future as the necessity comes".
  • 25. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    Infiniti wrote:
    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.
    How and why does cloning give you an advantage over simply creating a new "search form"?
  • 26. Re: is this write way to clone a object?
    800578 Newbie
    Currently Being Moderated
    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).
  • 27. Re: is this write way to clone a object?
    807591 Newbie
    Currently Being Moderated
    Infiniti wrote:
    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).
    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!

    So why the heck is this:
    Page old = browser.getPage();
    cache.add(old);
    Page newPage = old.clone();
    browser.setPage(newPage);
    better than this?
    Page old = browser.getPage();
    cache.add(old);
    Page newPage = new Page(browser.getLink());
    browser.setPage(newPage);
    (This is all fake code). I'm trying to figure out why you need to clone.
  • 28. Re: is this write way to clone a object?
    800578 Newbie
    Currently Being Moderated
    Any how why do i need the clone is not the problem but how to do is the problem.
1 2 Previous Next