モジュラーイベントハンドリングシステムの設計は、保守可能な拡張可能なJavaFXアプリケーションを作成するために不可欠です。Mediatorパターンは、コンポーネントをデカップリングし、それら間の通信を合理化するための効果的な方法を提供します。この記事では、JavaFXのMediatorパターンを使用して、モジュラーイベントハンドリングシステムを実装する方法について説明します。

媒体者パターンの理解

仲介者パターンは、コンポーネントが直接互いに通信するのではなく、中央の仲介者を通して通信することを保証することによって、緩いカップリングを促進する行動設計パターンです。JavaFXでは、このパターンは、UIコンポーネント間の複雑な相互作用を管理し、コードを維持し、拡張するのが容易になります。

媒体子インターフェイスの設計

コンポーネントやイベントの処理方法を宣言する仲介者インターフェイスを定義し始めます。このインターフェイスは、仲介者の行動の契約として機能します。

public interface EventMediator {
 void registerComponent(String name, UIComponent component);
 void handleEvent(String eventType, String sourceComponent);
}

媒体者を実装する

コンクリートの仲介者は登録されたコンポーネントを管理し、イベントの処理を調整します。コンポーネントのマップを維持し、さまざまなイベントに対応する方法を定義します。

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);
 }
 }
}

UIコンポーネントの作成

ボタンやスライダなどのUIコンポーネントは、それぞれと直接の代わりに、仲介者と通信する必要があります。各コンポーネントはイベントが発生したときに仲介者を通知します。

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);
 }
}

JavaFXのコンポーネントとMediatorを統合

JavaFX アプリケーションでは、仲介者とコンポーネントをインスタンス化し、仲介者とコンポーネントを登録し、イベントハンドラを設定して、仲介者通知をトリガーします。

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);
 }
}

このアプローチを追って、Mediator パターンを使用して、JavaFX アプリケーションでコンポーネントの相互作用を簡素化する、モジュール式でスケーラブルなイベント処理システムを作成します。