1

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

CJCrafter
  • 75
  • 1
  • 11
  • Your current `Goomba` class is already on the right track, and when the Goomba dies it should notify all listeners. I don't see any code which creates listeners, and passes to them a `Goomba` reference. – Tim Biegeleisen Apr 01 '19 at 15:47
  • `this makes Goomba the observer` - no, `Goomba` is the observable. `GoombdaDeathListener` is the observer. An observer is one who observes an observable. An observable is something which can be observed by another. – Brandon Apr 01 '19 at 15:49

1 Answers1

5

What am I not understanding of the Observer Pattern

Your GoobmaDeathListener is the Observer. It is observing events on the observable, your Goomba.

You might want to look at a more detailed explanation here: https://stackoverflow.com/a/13744816/340088

how can I have multiple Subjects(Goomba) call onGoombaDeath(this) when they die

Well each Goomba (Observable) must have a reference to the list of Observers. Most probably you would want to introduce a class in the middle, called something like GoombaObservers which manages the list of interested Observers. It would offer a method like notifyGoombaDeath() which internally does the loop to call onGoombaDeath().

You might also want to see what Java already offers out of the box with java.util.Observable.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • Would it best to then make this "middle class" static then? I can't make an instance of it in the Goomba class because it would create the original issue. (I ask as I hate static) – CJCrafter Apr 01 '19 at 16:53
  • Well ideally not static. Would it be a problem to pass it as a constructor argument to each `Goomba`? (If you are using some framework like Spring it would automatically inject it for you too through the constructor.)You would then be able to add or remove Observers to the `GoombaObservers` class dynamically, and all `Goomba` instances would be automatically notifying everyone. If you really can't pass it, you can also use the Singleton pattern to initialise the `GoombaObservers` instance once and only once throughout your program (the static method would just get the same singleton instance). – jbx Apr 01 '19 at 20:39