Answer the question
In order to leave comments, you need to log in
Why does a java.lang.IllegalMonitorStateException occur when building the mediator pattern?
I'm trying to create a mediator pattern
. There is a Mediator interface:
package Eighth.mediator;
public interface Mediator {
void notify(Components components);
}
package Eighth.mediator;
public class ConcreteMediator implements Mediator{
@Override
public void notify(Components components) {
System.out.println("User pressed " + components.getName());
}
}
package Eighth.mediator;
public interface Components {
String getName();
void onPress();
}
package Eighth.mediator;
public class Button implements Components{
public Mediator mediator;
private String name;
public Button(String name) {
this.name = name;
mediator = new ConcreteMediator();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void onPress(){
mediator.notify();
}
}
package Eighth.mediator;
public class Main {
public static void main(String[] args) {
Components button1 = new Button("Button1");
Components button2 = new Button("Button2");
Components checkbox1 = new Checkbox("Checkbox1");
Components radioButton1 = new RadioButton("Radiobutton1");
Components checkbox2 = new Checkbox("Checkbox2");
button1.onPress();
button2.onPress();
}
}
Answer the question
In order to leave comments, you need to log in
Because you are calling the .notify() method from the Object and not from the Mediator
like this:
public void onPress(){
mediator.notify();
}
void notify(Components components);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question