You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/Observer Pattern.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,3 +219,74 @@ public class ObserverPattern {
219
219
```
220
220
---
221
221
## Real World Example
222
+
223
+
The **Observer Pattern** is commonly used in many libraries and frameworks. For example, the **Swing Library** uses the Observer Pattern in the `JButton` class, which has various action listeners that are triggered when the button is clicked.
224
+
225
+
This class allows you to **add or remove observers** easily. You can also create multiple listeners by implementing the `ActionListener` interface.
226
+
227
+
Below is an example demonstrating this.
228
+
```java title:SwingApp.java
229
+
packageobserver;
230
+
231
+
importjava.awt.Rectangle;
232
+
importjava.awt.event.ActionEvent;
233
+
importjava.awt.event.ActionListener;
234
+
importjavax.swing.JButton;
235
+
importjavax.swing.JFrame;
236
+
237
+
// It is mostly used in Swing application
238
+
classSwingApp {
239
+
privateJFrame jframe;
240
+
241
+
publicSwingApp() {
242
+
jframe =newJFrame("Swing Example");
243
+
jframe.setVisible(true);
244
+
jframe.setBounds(newRectangle(50, 50, 400, 400));
245
+
246
+
}
247
+
248
+
publicvoiddefineButton() {
249
+
JButton button =newJButton("Click Me");
250
+
button.setBounds(10, 10, 50, 30);
251
+
jframe.add(button);
252
+
253
+
// we are subscribing the observer when the button is clicked.
254
+
button.addActionListener(newAngelListener());
255
+
}
256
+
257
+
}
258
+
259
+
// These is the observer
260
+
classAngelListenerimplementsActionListener {
261
+
262
+
@Override
263
+
publicvoidactionPerformed(ActionEvente) {
264
+
System.out.println(e.getActionCommand());
265
+
}
266
+
267
+
}
268
+
269
+
publicclassSwingApplication {
270
+
271
+
publicstaticvoidmain(String[] args) {
272
+
SwingApp app =newSwingApp();
273
+
app.defineButton();
274
+
}
275
+
276
+
}
277
+
278
+
```
279
+
**Output:**
280
+
281
+
![[Pasted image 20250212233819.png|400]]
282
+
283
+
When the button is clicked, the text **"Click Me"** appears in the console. When you click on the `JButton`, it **notifies the observers** subscribed to it that a click event has occurred, prompting them to execute their respective code.
284
+
285
+
---
286
+
## Design Principles
287
+
288
+
-**Encapsulate What Varies** - Identify the parts of the code that are going to change and encapsulate them into separate class just like the Strategy Pattern.
289
+
-**Favor Composition Over Inheritance** - Instead of using inheritance on extending functionality, rather use composition by delegating behavior to other objects.
290
+
-**Program to Interface not Implementations** - Write code that depends on Abstractions or Interfaces rather than Concrete Classes.
291
+
-**Strive for Loosely coupled design between objects that interact** - When implementing a class, avoid tightly coupled classes. Instead, use loosely coupled objects by leveraging abstractions and interfaces. This approach ensures that the class does not heavily depend on other classes.
0 commit comments