Object clone

C

Colin Hemmings

Hi there,
I have a game that I am currently developing and I am looking to make
exact copies of the gameState object throughout the run of the game. I
need to do this so that I can do move 'look ahead' but not effect the
actual state of the game.

I was looking at using the clone method of Object, but the API says that
clone only performs a "shallow copy" of the object. The object I will be
looking to clone contains a collection of other objects. I want to make
an exact clone of the gameState object that is completely independent of
the original.

Will clone do this or is there a better way?
 
J

Joe Attardi

Colin said:
I was looking at using the clone method of Object, but the API says that
clone only performs a "shallow copy" of the object. The object I will be
looking to clone contains a collection of other objects. I want to make
an exact clone of the gameState object that is completely independent of
the original.

clone won't do this for you. A shallow copy will still have references
to the same underlying objects, so if you change a mutable object in
the cloned one, the original one will be updated too.
 
T

Thomas Weidenfeller

Colin said:
I was looking at using the clone method of Object, but the API says that
clone only performs a "shallow copy" of the object.

It is up to you what a clone() method implementation does. If you need a
deep copy, implement clone() that way.
Will clone do this or is there a better way?

Since you have to implement clone() in your class it will do almost
whatever you want. Almost, because the tricky part is that if you need
to call the clone() methods of existing objects (e.g. collections) or
your superclass, you need to know how these methods are implemented. If
these existing clone() methods only provide a shallow copy, you need to
construct a deep copy of objects in your own code.

You can only do this if the APIs of the existing classes allow yo to
peek at enough of their internal state to construct a deep copy. If they
don't, you probably get enough insight when you subclasses the
particular classes. If the particular classes are final you are in
trouble. Then it would be time for some ugly reflection hack or for
betting on serialization.

/Thomas
 
O

Oliver Wong

Colin Hemmings said:
Hi there,
I have a game that I am currently developing and I am looking to make
exact copies of the gameState object throughout the run of the game. I
need to do this so that I can do move 'look ahead' but not effect the
actual state of the game.

I was looking at using the clone method of Object, but the API says that
clone only performs a "shallow copy" of the object. The object I will be
looking to clone contains a collection of other objects. I want to make an
exact clone of the gameState object that is completely independent of the
original.

Will clone do this or is there a better way?

You might want to look into making your GameState objects into something
immutable, so as to sidestep the whole issue alltogether. Have it work like
BigInteger or String, such that when you perform an operation (such as "move
player 1 forward 1 square"), rather than modifying the current GameState, it
returns a new GameState object representing the new move.

- Oliver
 
R

Roedy Green

I was looking at using the clone method of Object, but the API says that
clone only performs a "shallow copy" of the object. The object I will be
looking to clone contains a collection of other objects. I want to make
an exact clone of the gameState object that is completely independent of
the original.

if you serialize an object, you get a deep copy. If you restore it,
you have an reasonably independent clone. Some of the strings might be
common, but that won't cause trouble.

It is an easy way to get a deep copy, albeit with a high overhead.

see http://mindprod.com/jgloss/serialization.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top