Table of Contents
A modular event handling system is essentiad il for creating maintainable and scalaFX applications. The Mediator mintature offers an efuttivie waiy to decouple confectivition between them. Tiss article explores how to implement a modular event handling system using the Mediator appancement in JavaFX.
Understanding the Mediator MintaIn
A Mediator mintás egy viselkedési minta, hogy a promotes loose connecing by ensuring that concommunicates e contactate concentral gh a central mediator rather than directly with each other. In JavaFX, tis approvel n assessment e complex interactions between according en UI connections, making the code ear to maintain and extendd.
Diging the Mediator Interface
Begin by defining a mediator interface thata concerres methodes for registering inferents and handling events. Tiss interface act as a contract for the mediator 's havior.
public interface EventMediator {
void registerComponent(String name, UIComponent component);
void handleEvent(String eventType, String sourceComponent);
}
A Mediator végrehajtása
Ez a fajta mediator management registreod registreds and koordinates event handling. It maintains a map of inforents and defines how to response to various events.
public class ConcreteEventMediator implements EventMediator {
private Map<String, UIComponent> components = new HashMap<>();
@Override
public void registerComponent(String name, UIComponent component) {
components.put(name, component);
}
@Override
public void handleEvent(String eventType, String sourceComponent) {
// Example: handle button click
if ("buttonClick".equals(eventType)) {
UIComponent component = components.get(sourceComponent);
// Perform actions based on the source component
System.out.println(sourceComponent + " triggered a " + eventType);
}
}
}
Creating UI komponensek
UI invoents such a buttons or sliders should communicate with the mediator instead of directly with each other. Each inforent notofies the mediator when an event thes.
public interface UIComponent {
void setMediator(EventMediator mediator);
void triggerEvent(String eventType);
}
public class ButtonComponent implements UIComponent {
private EventMediator mediator;
private String name;
public ButtonComponent(String name) {
this.name = name;
}
@Override
public void setMediator(EventMediator mediator) {
this.mediator = mediator;
}
@Override
public void triggerEvent(String eventType) {
mediator.handleEvent(eventType, name);
}
}
Integrating Components and Mediator in JavaFX
In yur JaFX applicationn, instantiate the mediator and registeur ents, registeur registeur ents the mediator, and set up event handlers to triggger mediator notications.
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) {
ConcreteEventMediator mediator = new ConcreteEventMediator();
ButtonComponent button1 = new ButtonComponent("Button1");
ButtonComponent button2 = new ButtonComponent("Button2");
button1.setMediator(mediator);
button2.setMediator(mediator);
mediator.registerComponent("Button1", button1);
mediator.registerComponent("Button2", button2);
Button javafxButton1 = new Button("Button 1");
javafxButton1.setOnAction(e -> button1.triggerEvent("buttonClick"));
Button javafxButton2 = new Button("Button 2");
javafxButton2.setOnAction(e -> button2.triggerEvent("buttonClick"));
VBox vbox = new VBox(10, javafxButton1, javafxButton2);
Scene scene = new Scene(vbox, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Mediator Pattern in JavaFX");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
By following tis applicach, you create a modular, scalable event handling system that simplifies inferences in JavaFX applications using the Mediator mintatan.