0

-The problem-

In a game (Model-View-Controller design) that I'm programming to practice with the (optimization?) of object creation, I want to create an object, and use that object to create clones. I do not want the clone's addresses to reference to the original object.

By what means can I create a series of clones that do not reference to the original object? (I believe the terminology is called "dereference".)

-In my code-

I have an EntityLibrary which instantiates and stores one instance of each kind of Entity child that I have (ie. Cat, Dog, Fish). Each individual Entity child in the EntityLibrary is meant to be retrieved by the Model class when the Model class decides that it needs a clone of that Entity child.

The problem that I'm running into is that the address of the Entity child's clone references to the same address of the original Entity child located in the EntityLibrary.

Andrew
  • 79
  • 3
  • 12
  • Do you want a [_deep clone_](http://stackoverflow.com/questions/6182565/deep-copy-shallow-copy-clone)? – Sotirios Delimanolis Nov 05 '15 at 18:05
  • Please explain further as to what a deep clone is. – Andrew Nov 05 '15 at 18:06
  • Explain further. I require explanations. Beep beep. – Kayaman Nov 05 '15 at 18:08
  • 1
    This will not optimize object creation. Since object creation is at the core of Java they have optimized it more than you can. If you have a initial set of values that you want all objects from the same class to start with just put that in the constructor. – markbernard Nov 05 '15 at 18:09
  • You only want to pool objects that take a long time to create, such as a database connection object. – markbernard Nov 05 '15 at 18:10
  • You're describing the [prototype pattern](https://en.wikipedia.org/wiki/Prototype_pattern). It's not used for optimization though, you can forget all about that. It's used for, well creating objects based on one or more already created objects. – Kayaman Nov 05 '15 at 18:11
  • I decided to create a pool of objects only because each object requires a series of images. It creates lag if the images are constantly called, so I made an ImageLibrary as well. Was that the correct thing to do? – Andrew Nov 05 '15 at 18:12
  • What good is the pool? The images are of course shared, so a pool doesn't help. – Kayaman Nov 05 '15 at 18:13
  • 1
    You can use the clone() method of Object, if, as per java spec, the properties of your entity is primitive immutable type. For mutable types, best practice is to create new instances and attach it to the cloned object reference. – Aninda Bhattacharyya Nov 05 '15 at 18:16
  • 1
    You don't need a pool of anything. You need to load the images once, then have all the objects use them. Since you're not modifying the images, it doesn't matter if multiple game objects refer to the same image. – Kayaman Nov 05 '15 at 18:17
  • But where, if not at all, would I store the references to the images if I don't store the reference into a pool? I can't create 7000 entities and load the same image 7000 times, as that causes a ton of lag. – Andrew Nov 05 '15 at 18:20
  • Thank you for all the replies, by the way. This might be frustrating for you guys, but it's helping me a lot. – Andrew Nov 05 '15 at 18:22
  • 1
    No, only a fool would load the same image more than once. You load them once, put them into a data structure of your choice, such as a list or a map or even an array. A pool is a different beast, with borrow/return semantics needed to make sure that only one thread uses the particular pooled object (such as a db connection) at once. You don't need to worry about 7000 game objects using the same image at the same time. – Kayaman Nov 05 '15 at 18:25
  • Perhaps pool was the incorrect term, then. To clear things up, let me explain what I meant by "pool": I stored the address of each image required in future use to specified BufferedImage variables in a class called ImageLibrary. When necessary, I pull the image from the library and store the reference into whatever object requires it. – Andrew Nov 05 '15 at 18:32
  • My hope was that if I did the same thing for my Entities, I could just (shallow?)copy the original variables and mutate the variables of this new Entity independently of the original Entity. – Andrew Nov 05 '15 at 18:37
  • 1
    I don't see why you need to copy anything. You can easily do something like `Enemy e = new Enemy(x, y, image);` (with x, y, and image some relevant values, `image` being an `id` of some sort for example). I definitely don't recommend attempting to work with `clone()`. – Kayaman Nov 05 '15 at 18:39
  • 1
    Actually, not an image id, because that would make `Enemy` responsible for fetching its own image, which isn't sound design. It would be something like `new Image(x, y, ImageLibrary.getImage(id));`. – Kayaman Nov 05 '15 at 18:46
  • Ah, yes, that was the workaround which I needed to spark another idea! Thanks, I'll get back to you in a little while to let you know if my idea works. – Andrew Nov 05 '15 at 18:50
  • I used created a call within my EntityLibrary class (which that holds an instance of each type of Entity) that returns whichever new Entity type that I need. The precreated Entities in the EntityLibrary class will just be considered Default entities for testing. Thanks so much, Kayaman! – Andrew Nov 05 '15 at 19:05

1 Answers1

0

Try to use the Cloneable interface

http://coderevisited.com/cloneable-interface-in-java/

  • No, don't. It's best to forget `clone()`, especially since there's no need for any sort of cloning/copying in this situation. – Kayaman Nov 05 '15 at 18:55