Skip to content

Commit 262a03c

Browse files
authored
Merge pull request #2 from darkhyper24/bloC-issue
Bloc issue
2 parents b4c862a + fb6132c commit 262a03c

File tree

16 files changed

+81
-73
lines changed

16 files changed

+81
-73
lines changed

bloC/src/main/java/com/iluwatar/bloc/Main.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

bloC/src/main/java/com/iluwatar/bloc/State.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

bloC/Readme.md renamed to bloc/Readme.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ The Bloc pattern manages the state of an object and allows for dynamically notif
3939

4040
## Programmatic Example of the Bloc Pattern in Java
4141

42-
### 1. `Troll` Analogy Using Bloc
43-
44-
Imagine a simple state-driven system that maintains a counter. Whenever the counter changes, any registered listener receives updates dynamically.
45-
46-
---
47-
4842
### **Core Components of the Bloc Pattern**
4943

5044
#### **1. State Object**
File renamed without changes.
File renamed without changes.

bloC/pom.xml renamed to bloc/pom.xml

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
<artifactId>java-design-patterns</artifactId>
99
<version>1.26.0-SNAPSHOT</version>
1010
</parent>
11-
<artifactId>bloC</artifactId>
12-
<properties>
13-
<maven.compiler.source>17</maven.compiler.source>
14-
<maven.compiler.target>17</maven.compiler.target>
15-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
</properties>
11+
<artifactId>bloc</artifactId>
1712
<dependencies>
1813
<dependency>
1914
<groupId>org.junit.jupiter</groupId>
@@ -23,23 +18,51 @@
2318
<dependency>
2419
<groupId>org.testng</groupId>
2520
<artifactId>testng</artifactId>
26-
<version>RELEASE</version>
27-
<scope>test</scope>
28-
</dependency>
29-
<dependency>
30-
<groupId>junit</groupId>
31-
<artifactId>junit</artifactId>
21+
<version>7.7.1</version>
3222
<scope>test</scope>
3323
</dependency>
3424
<dependency>
3525
<groupId>org.assertj</groupId>
3626
<artifactId>assertj-core</artifactId>
27+
<version>3.24.2</version>
3728
<scope>test</scope>
3829
</dependency>
3930
<dependency>
4031
<groupId>org.mockito</groupId>
4132
<artifactId>mockito-inline</artifactId>
4233
<scope>test</scope>
4334
</dependency>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
4440
</dependencies>
45-
</project>
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-assembly-plugin</artifactId>
46+
<configuration>
47+
<archive>
48+
<manifest>
49+
<mainClass>com.iluwatar.bloC.App</mainClass>
50+
</manifest>
51+
</archive>
52+
<descriptorRefs>
53+
<descriptorRef>jar-with-dependencies</descriptorRef>
54+
</descriptorRefs>
55+
</configuration>
56+
<executions>
57+
<execution>
58+
<id>make-assembly</id>
59+
<phase>package</phase>
60+
<goals>
61+
<goal>single</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
</project>

bloC/src/main/java/com/iluwatar/bloc/Bloc.java renamed to bloc/src/main/java/com/iluwatar/bloc/Bloc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ private void emitState(State newState) {
6868
* Increments the current state value by 1 and notifies listeners of the change.
6969
*/
7070
public void increment() {
71-
emitState(new State(currentState.getValue() + 1));
71+
emitState(new State(currentState.value() + 1));
7272
}
7373

7474
/**
7575
* Decrements the current state value by 1 and notifies listeners of the change.
7676
*/
7777
public void decrement() {
78-
emitState(new State(currentState.getValue() - 1));
78+
emitState(new State(currentState.value() - 1));
7979
}
8080
}

bloC/src/main/java/com/iluwatar/bloc/BlocUi.java renamed to bloc/src/main/java/com/iluwatar/bloc/BlocUi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void createAndShowUi() {
4141
frame.add(toggleListenerButton, BorderLayout.EAST);
4242

4343
// making a state listener to update the counter label when the state changes
44-
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
44+
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.value());
4545

4646
// adding the listener to the Bloc instance
4747
bloc.addListener(stateListener);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar.bloc;
2+
3+
/**
4+
* The BLoC (Business Logic Component) pattern is a software design pattern primarily used
5+
* in Flutter applications. It facilitates the separation of business logic from UI code,
6+
* making the application more modular, testable, and scalable. The BLoC pattern uses streams
7+
* to manage the flow of data and state changes, allowing widgets to react to new states as
8+
* they arrive.
9+
* In the BLoC pattern, the application is divided into three key components:
10+
* - Input streams: Represent user interactions or external events fed into the BLoC.
11+
* - Business logic: Processes the input and determines the resulting state or actions.
12+
* - Output streams: Emit the updated state for the UI to consume.
13+
* The BLoC pattern is especially useful in reactive programming scenarios and aligns well with the declarative nature of Flutter.
14+
* By using this pattern, developers can ensure a clear separation of concerns, enhance reusability, and maintain consistent state management throughout the application.
15+
*/
16+
17+
public class Main {
18+
19+
/**
20+
* The entry point of the application. Initializes the GUI.
21+
*
22+
* @param args command-line arguments (not used in this example)
23+
*/
24+
public static void main(String[] args) {
25+
BlocUi blocUi = new BlocUi();
26+
blocUi.createAndShowUi();
27+
}
28+
}

0 commit comments

Comments
 (0)