Introduction to Event-Driven Model: From Procedural to Reactive Java Code
Introduction to Event-Driven Model: From Procedural to Reactive Java Code
Event-Driven Model क्या होता है?
Event-Driven Model एक ऐसा programming approach है जिसमें पूरा program "events" पर आधारित होता है। यानी जब कोई action होता है — जैसे button click, mouse move, या key press — तो उसके अनुसार code execute होता है। Java में ये approach GUI applications, web applications और asynchronous systems में बहुत useful है।
इस model में code तब तक execute नहीं होता जब तक कोई event trigger न करे। इसे आप ऐसे समझ सकते हैं कि event-driven system "wait करता है" user interaction का, और फिर उसके response में काम करता है।
Procedural Programming vs Event-Driven Programming
Procedural programming में program sequentially चलता है — यानी step-by-step instructions एक fixed order में execute होती हैं। लेकिन event-driven programming में execution order fix नहीं होता, बल्कि events के आने पर dependent होता है।
| Feature | Procedural Programming | Event-Driven Programming |
|---|---|---|
| Execution Flow | Sequential (Step by Step) | Event-based (On Trigger) |
| Control | Programmer Control करता है flow | User या System events Control करते हैं |
| Use Case | Mathematical Calculations, Simple Scripts | GUI Applications, Games, Web Apps |
| Example | C, Pascal | Java Swing, JavaFX, JavaScript |
Java में Event-Driven Programming कैसे काम करता है?
Java में event-driven model को implement करने के लिए AWT (Abstract Window Toolkit) और Swing जैसे GUI frameworks का use किया जाता है। ये frameworks components जैसे button, checkbox, textbox आदि provide करते हैं, जिन पर user interaction possible होता है।
जब user कोई action perform करता है (जैसे button दबाना), तो एक event object generate होता है, और वो listener को notify करता है। Listener एक ऐसा object होता है जो किसी event को सुनता है और उसके अनुसार action perform करता है।
Java Event Handling Mechanism
Java का event handling model "delegation event model" कहलाता है। इस model में तीन main components होते हैं:
- Event Source: वो object जो event generate करता है (जैसे Button, TextField)।
- Event Object: वो object जो event की जानकारी रखता है (जैसे ActionEvent, MouseEvent)।
- Event Listener: वो interface या class जो उस event को handle करता है।
Event Delegation Model का Flow
जब कोई user event generate करता है, जैसे button click, तो:
- Event source एक event object बनाता है।
- Event object listener तक भेजा जाता है।
- Listener predefined method के ज़रिए उस event को handle करता है।
Java Event Classes और Listener Interfaces
Java के AWT और Swing packages में कई predefined event classes और listener interfaces होते हैं। नीचे कुछ common examples दिए गए हैं:
| Event Class | Listener Interface | Event Type |
|---|---|---|
| ActionEvent | ActionListener | Button Click, Menu Selection |
| ItemEvent | ItemListener | Checkbox, List Selection |
| MouseEvent | MouseListener, MouseMotionListener | Mouse Click, Move, Drag |
| KeyEvent | KeyListener | Keyboard Press, Release |
| WindowEvent | WindowListener | Window Open, Close |
Example: Button Click Event Handling
आइए एक simple example देखें जहाँ button click होने पर message print होता है।
import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f = new Frame("Event Example");
Button b = new Button("Click Me");
b.setBounds(80, 100, 100, 40);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked!");
}
});
f.add(b);
f.setSize(300, 250);
f.setLayout(null);
f.setVisible(true);
}
}
ऊपर के example में:
- Button event source है।
- ActionEvent event object है।
- ActionListener listener है जो event को handle कर रहा है।
Anonymous Inner Class vs Separate Listener Class
Event handling दो तरीकों से की जा सकती है:
- Anonymous Inner Class से (जैसे ऊपर example में)
- या फिर एक अलग class बनाकर जो ActionListener implement करे।
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Handled by Separate Class");
}
}
Multiple Event Sources Handling
कभी-कभी एक ही listener को multiple components के लिए use किया जा सकता है। इसके लिए event source की पहचान करने के लिए getSource() method का use किया जाता है।
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1)
System.out.println("Button 1 Clicked");
else if (e.getSource() == button2)
System.out.println("Button 2 Clicked");
}
Event Adapter Classes
कई बार listener interfaces में multiple methods होती हैं, और हमें केवल एक या दो methods ही चाहिए होती हैं। ऐसे cases में "adapter classes" का use किया जाता है। ये classes listener interface को implement करती हैं और सभी methods का empty implementation देती हैं।
- MouseAdapter → MouseListener के लिए
- KeyAdapter → KeyListener के लिए
- WindowAdapter → WindowListener के लिए
import java.awt.event.*;
class MyMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
}
}
Event Handling in Swing
Swing Java का modern GUI toolkit है जो lightweight components provide करता है। Event handling concept यहाँ भी वही रहता है, बस components जैसे JButton, JTextField, JLabel आदि का use होता है।
import javax.swing.*;
import java.awt.event.*;
public class SwingEventDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Event Example");
JButton b = new JButton("Click");
b.setBounds(100, 100, 100, 40);
b.addActionListener(e -> JOptionPane.showMessageDialog(f, "Button Pressed!"));
f.add(b);
f.setSize(300, 250);
f.setLayout(null);
f.setVisible(true);
}
}
From Event-Driven to Reactive Programming
Event-driven model से आगे बढ़ते हुए modern Java applications अब reactive programming approach अपनाती हैं। Reactive programming asynchronous data streams और event flow को handle करती है।
Reactive programming में system हर event पर react करता है — चाहे वो user input हो, data update हो या external API response। Java में reactive programming के लिए Reactive Streams API और frameworks जैसे Project Reactor या RxJava use किए जाते हैं।
Reactive Programming के Core Principles
- Asynchronous Processing: Code non-blocking होता है।
- Event Streams: Data continuous stream के रूप में आता है।
- Backpressure Handling: Producer और Consumer के बीच load balance।
- Reactive Streams API: Publisher, Subscriber, Subscription, Processor components का use।
Reactive Example using RxJava
import io.reactivex.rxjava3.core.Observable;
public class ReactiveExample {
public static void main(String[] args) {
Observable source = Observable.just("Java", "Kotlin", "Spring");
source.subscribe(
item -> System.out.println("Received: " + item),
Throwable::printStackTrace,
() -> System.out.println("Completed")
);
}
}
ऊपर के code में, observable एक event stream represent करता है, और subscriber events को handle करता है — ठीक वैसे ही जैसे event-driven model में listener काम करता है।
Event-Driven vs Reactive Model Comparison
| Feature | Event-Driven Model | Reactive Model |
|---|---|---|
| Focus | User Interaction Events | Data Stream और Async Flow |
| Nature | Imperative | Declarative |
| Thread Blocking | Blocking | Non-blocking |
| Frameworks | AWT, Swing | RxJava, Reactor, Akka |
Advantages of Event-Driven Programming
- User friendly और interactive UI बनाना आसान होता है।
- Code modular और maintainable रहता है।
- Events handle करने से responsiveness बढ़ती है।
- Real-time applications के लिए ideal approach है।
Disadvantages of Event-Driven Model
- Debugging और flow tracking मुश्किल हो सकता है।
- Complex applications में event dependency बढ़ जाती है।
- Memory consumption ज़्यादा होती है।
Real-Life Use Cases
- Java GUI Applications (Swing, JavaFX)
- Gaming Applications (Event loops)
- IoT Devices (Sensor Events)
- Web Applications (Servlet-based event handling)
- Reactive Systems (Stock updates, Chat systems)
Summary Notes for Exam
- Event: कोई भी action या occurrence जो system को signal करे।
- Listener: वो object जो event handle करे।
- Delegation Model: Source → Event Object → Listener process।
- Adapter Class: Listener interface को simplify करने के लिए।
- Reactive Model: Event-driven का advanced asynchronous version।
- Example Frameworks: AWT, Swing, RxJava, Reactor।