I'm attempting to make a simple Mario game in java
. With this, I wanted a listener
for when a Goomba
(The main enemy in Mario) is killed. After a bit of research, I looked into the Observer Pattern
I made this interface
public interface GoombaDeathListener {
void onGoombaDeath(Goomba goomba);
}
Next I made myself a class
for my Player
public class Player extends Entity implements GoombaDeathListener {
@Override
public void onGoombaDeath(Goomba goomba) {. . .}
}
I also made a Goomba
class
public class Goomba extends Entity {
ArrayList<GoombaDeathListener> deathListeners = new ArrayList<>();
public void onDeath() {
for (GoombaDeathListener listener : deathListeners) {
listener.onGoombaDeath(this);
}
}
}
I believe(not sure if I'm in full understanding of this) that this makes Goomba
the observer
, and I want more than one Goomba
which means that I'd have multiple ArrayLists
of GoombaDeathListeners
So I have 2 Questions: What am I not understanding of the Observer Pattern
, and how can I have multiple Subjects
(Goomba
) call onGoombaDeath(this)
when they die