使用匿名实现时,无法理解如何阻止耐心的课程收听主题。
我尝试使用Subject变量,以便可以在reactToChange()中访问它;但不知道下一步该怎么做。
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");
}
}
}