Mediator Pattern
Mediator Pattern
- If the text in the TextBox exceeds a certain length, the button should be
disabled, regardless of the checkbox.
Without a mediator, the TextBox would need to directly interact with the
CheckBox and Button, and vice versa. If you added a new component,
say a **Label**, it would increase the complexity of communication,
requiring more direct interactions.
```java
// Mediator Interface
// Concrete Mediator
this.textBox = textBox;
this.checkBox = checkBox;
this.button = button;
@Override
button.setEnabled(true);
button.setEnabled(false);
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
// Colleague (TextBox)
this.mediator = mediator;
this.text = text;
mediator.notify(this, "textChanged");
return text;
// Colleague (CheckBox)
public class CheckBox {
this.mediator = mediator;
this.checked = checked;
return checked;
// Colleague (Button)
this.enabled = enabled;
return enabled;
```
### Conclusion