Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an object remove itself? How?

Tags:

java

object

I'm trying to write a simple ball game, and there's several turns (ie., ball lives). The ball "dies" when it passes the bottom border of the screen. What I have so far works, but doesn't seem to be the proper way to do things:

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

The die() method basically fades the ball's colour slowly (dark_gray -> pause(50) -> light_gray -> pause(50)), but doesn't actually do anything useful.

The remove(), obviously, gets rid of the ball from the screen, which is what I want. It makes sense to me for this remove() to be a part of Ball's die() method, as opposed to it being a separate method call in the main program -- but I'm not sure how to go about this?

Can an object delete itself? And, if it can, is object suicide better than object murder, from a philosophical/methodological point of view?

Thanks!

like image 922
Alicja Z Avatar asked Sep 01 '11 12:09

Alicja Z


People also ask

Can an object destroy itself?

A self-destruct is a mechanism that can cause an object to destroy itself or render itself inoperable after a predefined set of circumstances has occurred. Self-destruct mechanisms are typically found on devices and systems where malfunction could endanger large numbers of people.

How the object is destroyed in Java?

Java (and JVM in particular) uses automatic garbage collection. To put it simply, whenever new objects are created, the memory is automatically allocated for them. Consequently, whenever the objects are not referenced anymore, they are destroyed and their memory is reclaimed.

How do you delete an object in Java?

The remove(Object obj) method of List interface in Java is used to remove the first occurrence of the specified element obj from this List if it is present in the List. Parameters: It accepts a single parameter obj of List type which represents the element to be removed from the given List.


2 Answers

The object can remove itself given it has some sort of reference to the view rendering mechanism. Your sample doesn't give enough information so I'll exemplify one way to do it:

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 sample though, murder might be preferable since this way the Ball object doesn't need to know in which context it's being used.

like image 171
Johan Sjöberg Avatar answered Oct 14 '22 19:10

Johan Sjöberg


It is possible to create a method in which the ball removes itself, but it's a bad thing to do. In order to remove itself from the screen, the Ball must have a reference to the screen. This creates a circular chain of references (Ball has a reference to screen, screen has a reference to Ball) which is going to make your design more complicated and your testing much more complicated.

Suicide is fine - the screen tells the ball to die, and the ball dies. But this is about removal of a relationship, not dying. The thing maintaining the relationship is the screen, and so it should be the thing doing the removal.

Also remember that the two do not necessarily have to happen together. The screen might want to keep a dead ball around for some reason, and it might want to remove a ball that isn't dead. Even if that doesn't happen in your app right now, allow for the possibility.

like image 33
DJClayworth Avatar answered Oct 14 '22 21:10

DJClayworth