Skip to content

Commit 14b4c3a

Browse files
Completed Observer Pattern
1 parent 1414ffb commit 14b4c3a

File tree

5 files changed

+138
-3
lines changed

5 files changed

+138
-3
lines changed
28.4 KB
Loading

content/Observer Pattern.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,74 @@ public class ObserverPattern {
219219
```
220220
---
221221
## 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+
package observer;
230+
231+
import java.awt.Rectangle;
232+
import java.awt.event.ActionEvent;
233+
import java.awt.event.ActionListener;
234+
import javax.swing.JButton;
235+
import javax.swing.JFrame;
236+
237+
// It is mostly used in Swing application
238+
class SwingApp {
239+
private JFrame jframe;
240+
241+
public SwingApp() {
242+
jframe = new JFrame("Swing Example");
243+
jframe.setVisible(true);
244+
jframe.setBounds(new Rectangle(50, 50, 400, 400));
245+
246+
}
247+
248+
public void defineButton() {
249+
JButton button = new JButton("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(new AngelListener());
255+
}
256+
257+
}
258+
259+
// These is the observer
260+
class AngelListener implements ActionListener {
261+
262+
@Override
263+
public void actionPerformed(ActionEvent e) {
264+
System.out.println(e.getActionCommand());
265+
}
266+
267+
}
268+
269+
public class SwingApplication {
270+
271+
public static void main(String[] args) {
272+
SwingApp app = new SwingApp();
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.
292+
---
28.4 KB
Loading

0 commit comments

Comments
 (0)