Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions bloC/src/main/java/com/iluwatar/bloc/Main.java

This file was deleted.

27 changes: 0 additions & 27 deletions bloC/src/main/java/com/iluwatar/bloc/State.java

This file was deleted.

6 changes: 0 additions & 6 deletions bloC/Readme.md → bloc/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ The Bloc pattern manages the state of an object and allows for dynamically notif

## Programmatic Example of the Bloc Pattern in Java

### 1. `Troll` Analogy Using Bloc

Imagine a simple state-driven system that maintains a counter. Whenever the counter changes, any registered listener receives updates dynamically.

---

### **Core Components of the Bloc Pattern**

#### **1. State Object**
Expand Down
File renamed without changes
File renamed without changes.
49 changes: 36 additions & 13 deletions bloC/pom.xml → bloc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>
<artifactId>bloC</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<artifactId>bloc</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -23,23 +18,51 @@
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.bloC.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ private void emitState(State newState) {
* Increments the current state value by 1 and notifies listeners of the change.
*/
public void increment() {
emitState(new State(currentState.getValue() + 1));
emitState(new State(currentState.value() + 1));
}

/**
* Decrements the current state value by 1 and notifies listeners of the change.
*/
public void decrement() {
emitState(new State(currentState.getValue() - 1));
emitState(new State(currentState.value() - 1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void createAndShowUi() {
frame.add(toggleListenerButton, BorderLayout.EAST);

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

// adding the listener to the Bloc instance
bloc.addListener(stateListener);
Expand Down
28 changes: 28 additions & 0 deletions bloc/src/main/java/com/iluwatar/bloc/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.iluwatar.bloc;

/**
* The BLoC (Business Logic Component) pattern is a software design pattern primarily used
* in Flutter applications. It facilitates the separation of business logic from UI code,
* making the application more modular, testable, and scalable. The BLoC pattern uses streams
* to manage the flow of data and state changes, allowing widgets to react to new states as
* they arrive.
* In the BLoC pattern, the application is divided into three key components:
* - Input streams: Represent user interactions or external events fed into the BLoC.
* - Business logic: Processes the input and determines the resulting state or actions.
* - Output streams: Emit the updated state for the UI to consume.
* The BLoC pattern is especially useful in reactive programming scenarios and aligns well with the declarative nature of Flutter.
* By using this pattern, developers can ensure a clear separation of concerns, enhance reusability, and maintain consistent state management throughout the application.
*/

public class Main {

/**
* The entry point of the application. Initializes the GUI.
*
* @param args command-line arguments (not used in this example)
*/
public static void main(String[] args) {
BlocUi blocUi = new BlocUi();
blocUi.createAndShowUi();
}
}
8 changes: 8 additions & 0 deletions bloc/src/main/java/com/iluwatar/bloc/State.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.iluwatar.bloc;

/**
* The {@code State} class represents a state with an integer value.
* This class encapsulates the value and provides methods to retrieve it.
*/
public record State(int value) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ void initialState() {

@Test
void IncrementUpdateState() {
bloc.addListener(state -> stateValue.set(state.getValue()));
bloc.addListener(state -> stateValue.set(state.value()));
bloc.increment();
assertEquals(1, stateValue.get(), "State should increment to 1");
}

@Test
void DecrementUpdateState() {
bloc.addListener(state -> stateValue.set(state.getValue()));
bloc.addListener(state -> stateValue.set(state.value()));
bloc.decrement();
assertEquals(-1, stateValue.get(), "State should decrement to -1");
}
Expand All @@ -47,8 +47,8 @@ void removingListener() {
@Test
void multipleListeners() {
AtomicInteger secondValue = new AtomicInteger();
bloc.addListener(state -> stateValue.set(state.getValue()));
bloc.addListener(state -> secondValue.set(state.getValue()));
bloc.addListener(state -> stateValue.set(state.value()));
bloc.addListener(state -> secondValue.set(state.value()));
bloc.increment();
assertEquals(1, stateValue.get(), "First listener should receive state 1.");
assertEquals(1, secondValue.get(), "Second listener should receive state 1.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void setUp() {
frame.add(decrementButton, BorderLayout.SOUTH);
frame.add(toggleListenerButton, BorderLayout.EAST);

stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
stateListener = state -> counterLabel.setText("Counter: " + state.value());
bloc.addListener(stateListener);

incrementButton.addActionListener(e -> bloc.increment());
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
<module>function-composition</module>
<module>microservices-distributed-tracing</module>
<module>microservices-idempotent-consumer</module>
<module>bloC</module>
<module>bloc</module>

</modules>
<repositories>
Expand Down