0

Can't understand how to stop Patient class from listening Subject when using anonymous implementation.

I tried using Subject variable so I can acces it in reactToChange(); but could not figure out what to do next.

  import java.beans.PropertyChangeEvent;
    
    public class Patient
    {
      private int ticketNumber;
    
      public Patient(int ticketNumber, Subject subject)
      {
        this.ticketNumber = ticketNumber;    
        subject.addListener(this::reactToChange);  // implementing anonymous implementation
      }
    
      private void reactToChange(PropertyChangeEvent event)
      {
        int newValue = (Integer) event.getNewValue();    
        if(newValue == ticketNumber){
          System.out.println("Patient " + ticketNumber + " goes to the doctor's room");


          //need to delete this listener here

        } else {
          System.out.println("Patient " + ticketNumber + " goes back to looking at phone");
        }
      }
    
    }

Git hub link

Dorinus
  • 1
  • 1
  • Have you tried `subject.removeListener(this::reactToChange); `? – Polygnome Oct 19 '20 at 01:33
  • Hi, thx I don't know how I was writing it but it didn't compile. It compiles now, but it doesn't remove it as a listener. If you would like to check the code: [link](https://github.com/Dorinus/WaitingRoom) – Dorinus Oct 19 '20 at 02:17

0 Answers0