Go Back  DVD Talk Forum > General Discussions > Tech Talk
Reload this Page >

Easy object-oriented design q (Cloneable, deep vs. shallow)

Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

Easy object-oriented design q (Cloneable, deep vs. shallow)

Old 06-07-04, 10:43 PM
  #1  
DVD Talk Legend
Thread Starter
 
Join Date: Oct 1999
Location: |-|@><0r [email protected]|)
Posts: 17,214
Easy object-oriented design q (Cloneable, deep vs. shallow)

Sanity check for a homework assignment. What happens when you do the following?

1) Create two classes: one that extends a hashtable, and one that contains a hashtable as a member. (e.g.: EHash and CHash.)

2) In each class, implement methods "shallow clone" (one that returns a bit-for-bit copy of the hashtable) and "deep clone" (one that returns a copy of the hashtable, but with all references pointing to identical clones of the originally referenced objects.)

3) Instantiate one instance of each class. (e.g.: e and c.)

4) Create some random (cloneable) objects, and insert each one into both hashes.

5) For each of the instances created in #3, create a shallow clone (es and cs) and a deep clone (ed and cd.)

So, at this point, you have six objects:

e, its shallow clone es, and its deep clone ed;
c, its shallow clone cs, and its deep clone cd.

All six have identical-looking hashtables. But internally, e, c, es, and cs all reference the same object set, whereas cs and cd each reference its own cloned set of the object set. Easy so far.

Now:

6) Print the contents of hashtables es, ed, cs, and cd.

7) Clear the hashtables of e and c.

8) Print the contents of hashtables es, ed, cs, and cd again.

What do you see?

The results I received drove me crazy for an hour - until I realized that they were completely correct. My results: Hashtables es, ed, cs, and cd are all unchanged by clearing the hashtables in e and c.

Now, you'd expect that the point of the exercise would be to see some kind of differential results: something happening to the shallow copies that doesn't happen to the deep copies, or something happening to the subclass that doesn't happen to the container class. But that's not happening here, is it? es contains a clone of e's hashtable, not a reference to e's hashtable. So if e changes its hashtable, those changes aren't apparent in es. Now, if any of the objects in the e hashtable were altered, that would be visible in es (and c, and cs) - but not in ed (or cd.)

Is that correct? I'm going to throttle my professor tomorrow if so, so please let me know soon what you think.

- David Stein

Last edited by sfsdfd; 06-07-04 at 10:47 PM.
sfsdfd is offline  

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Thread Tools
Search this Thread

Archive Advertising Cookie Policy Privacy Statement Terms of Service

Copyright 2018 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.