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: bloc/README.md
+127-125
Original file line number
Diff line number
Diff line change
@@ -2,174 +2,177 @@
2
2
title: "Bloc Pattern in Java: State Management Simplified"
3
3
shortTitle: Bloc
4
4
description: "Learn how the Bloc pattern helps manage state changes in Java applications. This guide covers dynamic listener management, real-world examples, and clean code practices for state management."
5
-
category: Structural
5
+
category: Architectural
6
6
language: en
7
7
tag:
8
-
- Event-driven
8
+
- Abstraction
9
+
- Data binding
10
+
- Decoupling
11
+
- Event-driven
12
+
- Presentation
13
+
- Reactive
14
+
- Reusability
15
+
- State tracking
9
16
---
10
17
11
18
## Also known as
12
19
13
-
*Event-driven State Management
14
-
*State Listener Pattern
20
+
*Business Logic Component
21
+
*Business Logic Controller
15
22
16
23
## Intent of the Bloc Pattern
17
24
18
25
The Bloc pattern manages the state of an object and allows for dynamically notifying interested listeners about state changes. It separates state management logic from the rest of the application, improving code organization and flexibility.
19
26
20
27
## Detailed explanation of the Bloc pattern with real-World examples
21
28
22
-
### Real-world example
29
+
Real-world example
23
30
24
31
> Consider a digital counter application where multiple parts of the UI need to be updated whenever the counter changes. For example, a label displaying the counter value and an activity log showing changes. Instead of directly modifying these UI components, the Bloc pattern manages the counter state and notifies all registered listeners about the state change. Listeners can dynamically subscribe or unsubscribe from receiving updates.
25
32
26
-
### In plain words
33
+
In plain words
27
34
28
35
> The Bloc pattern manages a single state object and dynamically notifies registered listeners whenever the state changes.
29
36
30
-
### Wikipedia says
37
+
Wikipedia says
31
38
32
39
> While not a formalized "Gang of Four" design pattern, Bloc is widely used in state-driven applications. It centralizes state management and propagates state changes to registered observers, following principles of separation of concerns.
## Programmatic Example of the Bloc Pattern in Java
41
46
42
-
### **Core Components of the Bloc Pattern**
47
+
This example demonstrates how to implement the Bloc pattern using Java and Swing. The pattern separates the state of the application from UI components, and provides a reactive, event-driven approach to managing updates.
43
48
44
-
#### **1. State Object**
49
+
Core components of the Bloc Pattern include a `State` object, a `Bloc` class responsible for managing and updating that state, and interfaces (`StateListener` and `ListenerManager`) for subscribing to changes.
45
50
46
-
The `State` class holds the representation of the state of the application that will be passed to listeners whenever there is a change to do but in this example it's simplified to be a single value.
51
+
The `State` class represents the application's data at any given time.
47
52
48
53
```java
49
-
packagecom.iluwatar.bloc;
50
-
51
-
importlombok.Getter;
52
-
53
-
@Getter
54
-
publicclassState {
55
-
privatefinalint value;
54
+
public record State(int value) {}
55
+
```
56
56
57
-
publicState(intvalue) {
58
-
this.value = value;
59
-
}
57
+
The `ListenerManager` interface declares methods to add and remove listeners, as well as retrieve them.
60
58
61
-
}
62
-
```
63
-
The `ListenerManager` interface manages the basic operations for the listeners and is implemented by bloc class
64
59
```java
65
-
importjava.util.List;
66
-
67
60
publicinterfaceListenerManager<T> {
68
-
voidaddListener(StateListener<T>listener);
69
-
voidremoveListener(StateListener<T>listener);
70
-
List<StateListener<T>>getListeners();
61
+
voidaddListener(StateListener<T>listener);
62
+
voidremoveListener(StateListener<T>listener);
63
+
List<StateListener<T>>getListeners();
71
64
}
72
65
```
73
-
The `StateListener` interface has a method that the listener needs to react to changes in the state and is used by bloC to notify listeners whenever there is an update to state.
66
+
67
+
The `StateListener` interface defines how a listener reacts to state changes.
68
+
74
69
```java
75
70
publicinterfaceStateListener<T> {
76
-
voidonStateChange(Tstate);
71
+
voidonStateChange(Tstate);
77
72
}
78
73
```
79
74
80
-
The `Bloc` class holds the current state and manages logic of states and notifies the list of listeners when states changes.
81
-
The `Bloc` class contains methods for listeners and states like emitstate which updates the currentstate and notifies listeners addlistener which adds new listener to the listeners list and notifies it with the currentstate removelistener which removes listener from the listeners list and increment which increases the state value by 1 which is like an update to the current state and notifies the listeners in listeners list with the new state which holds a value incremented by 1 and decrement functions which does the opposite of increment function and notifies listeners in listeners list.
82
-
```java
83
-
importjava.util.ArrayList;
84
-
importjava.util.Collections;
85
-
importjava.util.List;
75
+
The `Bloc` class maintains the current state and notifies listeners of updates. It provides `increment` and `decrement` methods to update state and automatically notify registered listeners.
This class demonstrates how to integrate the Bloc pattern with a simple Swing GUI. It sets up a counter, buttons to change the state, and a toggle to enable or disable the listener dynamically.
110
121
111
-
privatevoidemitState(StatenewState) {
112
-
currentState = newState;
113
-
for (StateListener<State> listener : listeners) {
114
-
listener.onStateChange(currentState);
115
-
}
122
+
```java
123
+
publicclassMain {
124
+
publicstaticvoidmain(String[] args) {
125
+
BlocUi blocUi =newBlocUi();
126
+
blocUi.createAndShowUi();
127
+
}
116
128
}
117
129
118
-
publicvoidincrement() {
119
-
emitState(newState(currentState.getValue() +1));
120
-
}
130
+
publicclassBlocUi {
121
131
122
-
publicvoiddecrement() {
123
-
emitState(newState(currentState.getValue() -1));
124
-
}
125
-
}
126
-
```
127
-
The `main` class have a simple gui to try and test the bloc pattern components separately from the ui components.
128
-
the `main` class creates an instance of bloc then adds a listener to update the ui which resembles the counter and some buttons to change the states and toggle the listener dynamically
0 commit comments