Can an object delete itself? How?

I am trying to write a simple ball game, and there are several turns (i.e. the ball lives). The ball dies when it passes the bottom of the screen. That I am still working, but it seems, cannot be the right way:

if (ball.getY() > bottomOfScreen) { ball.die(); remove(ball); } 

The die () method basically gradually reduces the color of the ball (dark_gray → pause (50) → light_gray → pause (50)), but actually does nothing useful.

Remove (), obviously, get rid of the ball from the screen, what I want. It seems to me that this remove () is part of the Ball die () method, in contrast to the fact that it is a separate method call in the main program, but I'm not sure how to do it?

Can an object delete itself? And, if possible, is objective suicide better than killing an object from a philosophical / methodological point of view?

Thanks!

+7
source share
5 answers

An object can delete itself if it has a link to the rendering engine of the view. Your sample does not provide enough information, so I will demonstrate one way to do this:

 public class Ball { private ViewRenderer view; public void remove() { view.remove(this); } } 

Neither suicide nor murder is better or worse. It depends on your design and requirements.

In this example, however, killing may be preferable since this Ball object does not need to know in what context it is used.

+4
source

You can create a method in which the ball removes itself, but this is bad. To remove itself from the screen, Ball must have a link to the screen. This creates a circular chain of links (Ball has a link to the screen, there is a link to Ball on the screen), which will make your design more complex, and your testing will be much more complicated.

Suicide is beautiful - the screen says the ball is dying and the ball is dying. But this is about eliminating the relationship, not death. The thing that maintains the relationship is the screen, and therefore it should be what the removal does.

Also remember that these two do not have to happen together. For some reason, the screen may want to fix a dead ball, and it may want to remove a ball that is not dead. Even if this does not happen in your application right now, enable this feature.

+3
source

In the sense of removing an object from memory: no, in Java, which is handled exclusively by the garbage collector.

What you can do is remove the object from the collections containing it, but this requires that the object has access to these collections (which in most cases would be impossible, since there can be many collections).

I suggest containing an object (a screen in your case) to poll the state of the contents of the object (ball) and delete it after it is really dead.

+2
source

There is supposedly some object (for example, Screen or ViewRenderer in the Johan example) that contains a link to Ball, and the removal of this link should be done using the screen ("killing the object"). “Suicide target” means that Ball sent a message to the screen asking him to “kill”.

Since this is a Ball that knows when it crossed the border, it makes sense to me (without knowing the details of your design) that the removal be initiated by Ball. The screen can then find out about this change in one of several ways:

  • The screen can interrogate Ball.
  • The ball may contain a direct link back to the screen, which creates an unhappy circular dependency.
  • The ball may contain a link to the screen through the BallObserver interface. This is an observer pattern app.

The first is the simplest, and this makes it a good choice if it naturally fits into your screen-drawing mechanism. The third, in principle, is more flexible, but you may not need this flexibility in practice. The middle option may be OK in a simple program, but you should probably consider it as a step towards the third.

And if you don't have a Screen object (or ViewRenderer or something else) and really mean “a separate method call in the main program”, you should probably rethink your design.

+1
source

No, objects cannot be suicides. Any link in itself is just a link.

To “clear” an object within you, you simply clear all instance variables.

To “clear” an object outside of you, you must set the variable to zero.

0
source

All Articles