การออกแบบระบบจัดการเหตุการณ์ต่าง ๆ ที่จําเป็นสําหรับการสร้างโปรแกรมจาวา FX ที่สามารถรักษาได้ และสามารถพิมพ์ได้ รูปแบบความต่อเนื่องนี้จะให้วิธีการถอดรหัสส่วนประกอบการจําแนกและลดความคมชัดระหว่างเหตุการณ์เหล่านั้น บทความนี้ทําการสํารวจวิธีจัดการระบบการทํางานแบบ SOCKS โดยใช้รูปแบบการประมวลผลแบบ SOCKS ใน JavaFX

การ เข้าใจ รูป แบบ ของ คน กลาง

ใน ชวา ฟ ็กซ์ รูป แบบ นี้ ช่วย จัด การ กับ การ ติด ต่อ สัมพันธ์ ที่ ซับ ซ้อน ระหว่าง องค์ ประกอบ ของ ยูไอ ทํา ให้ รหัส นั้น คงทน และ ขยาย ตัว ได้ ง่าย ขึ้น

ออกแบบอินเทอร์เฟซ

เริ่มจากการกําหนดส่วนติดต่อแบบกลาง ซึ่งประกาศวิธีการในการลงทะเบียนส่วนประกอบและจัดการเหตุการณ์ต่าง ๆ ส่วนติดต่อนี้ทําหน้าที่เป็นสัญญาสําหรับพฤติกรรมของผู้กลาง

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

ส่วน ประกอบ ต่าง ๆ ของ ยูไอ เช่น ปุ่ม หรือ แถบ แถบ ตัว กระบอก ควร ติด ต่อ กับ ผู้ กลาง แทน ที่ จะ ติด ต่อ กัน โดย ตรง.

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

ส่วน ประกอบ ที่ กระตุ้น และ ผู้ กลาง ใน ชวา ฟอค

ในโปรแกรมจาวาเอฟเอกซ์ของคุณ แจ้งเตือนสื่อและส่วนประกอบ ลงทะเบียนส่วนประกอบกับสื่อ และตั้งผู้ดูแลเหตุการณ์ เพื่อเรียกตัวการแจ้งการกลาง

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

โดยวิธีการนี้ คุณสร้างระบบจัดการเหตุการณ์แบบ SOCKS แบบแยกประเภทได้ ซึ่งช่วยลดความปฏิสัมพันธ์ของส่วนประกอบในโปรแกรมจาวาเอฟเอกซ์ โดยใช้รูปแบบตัวกลาง