diff --git a/bloC/src/main/java/com/iluwatar/bloc/Main.java b/bloC/src/main/java/com/iluwatar/bloc/Main.java deleted file mode 100644 index 9751019c3c43..000000000000 --- a/bloC/src/main/java/com/iluwatar/bloc/Main.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.iluwatar.bloc; - -/** - * The Main class demonstrates the use of the Bloc pattern in a simple GUI application. - * It initializes the UI and sets up actions for the buttons and listener management. - */ -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(); - } -} \ No newline at end of file diff --git a/bloC/src/main/java/com/iluwatar/bloc/State.java b/bloC/src/main/java/com/iluwatar/bloc/State.java deleted file mode 100644 index fcf13287bcf0..000000000000 --- a/bloC/src/main/java/com/iluwatar/bloc/State.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.iluwatar.bloc; - -import lombok.Getter; - -/** - * The {@code State} class represents a state with an integer value. - * This class encapsulates the value and provides methods to retrieve it. - */ -@Getter -public class State { - /** - * -- GETTER -- - * Returns the value of the state. - * - */ - private final int value; - - /** - * Constructs a {@code State} with the specified value. - * - * @param value the value of the state - */ - public State(int value) { - this.value = value; - } - -} diff --git a/bloC/Readme.md b/bloc/Readme.md similarity index 97% rename from bloC/Readme.md rename to bloc/Readme.md index 01871fcfc4c8..228af1634052 100644 --- a/bloC/Readme.md +++ b/bloc/Readme.md @@ -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** diff --git a/bloC/etc/bloC.png b/bloc/etc/bloc.png similarity index 100% rename from bloC/etc/bloC.png rename to bloc/etc/bloc.png diff --git a/bloC/etc/bloC.puml b/bloc/etc/bloc.puml similarity index 100% rename from bloC/etc/bloC.puml rename to bloc/etc/bloc.puml diff --git a/bloC/pom.xml b/bloc/pom.xml similarity index 54% rename from bloC/pom.xml rename to bloc/pom.xml index eda6a71d6640..1edc49f03721 100644 --- a/bloC/pom.xml +++ b/bloc/pom.xml @@ -8,12 +8,7 @@ java-design-patterns 1.26.0-SNAPSHOT - bloC - - 17 - 17 - UTF-8 - + bloc org.junit.jupiter @@ -23,17 +18,13 @@ org.testng testng - RELEASE - test - - - junit - junit + 7.7.1 test org.assertj assertj-core + 3.24.2 test @@ -41,5 +32,37 @@ mockito-inline test + + junit + junit + test + - \ No newline at end of file + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + com.iluwatar.bloC.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + diff --git a/bloC/src/main/java/com/iluwatar/bloc/Bloc.java b/bloc/src/main/java/com/iluwatar/bloc/Bloc.java similarity index 94% rename from bloC/src/main/java/com/iluwatar/bloc/Bloc.java rename to bloc/src/main/java/com/iluwatar/bloc/Bloc.java index 62b629fa734b..caed7080ab1c 100644 --- a/bloC/src/main/java/com/iluwatar/bloc/Bloc.java +++ b/bloc/src/main/java/com/iluwatar/bloc/Bloc.java @@ -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)); } } diff --git a/bloC/src/main/java/com/iluwatar/bloc/BlocUi.java b/bloc/src/main/java/com/iluwatar/bloc/BlocUi.java similarity index 98% rename from bloC/src/main/java/com/iluwatar/bloc/BlocUi.java rename to bloc/src/main/java/com/iluwatar/bloc/BlocUi.java index 1cb0b0379de7..e74536c10c40 100644 --- a/bloC/src/main/java/com/iluwatar/bloc/BlocUi.java +++ b/bloc/src/main/java/com/iluwatar/bloc/BlocUi.java @@ -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 stateListener = state -> counterLabel.setText("Counter: " + state.getValue()); + StateListener stateListener = state -> counterLabel.setText("Counter: " + state.value()); // adding the listener to the Bloc instance bloc.addListener(stateListener); diff --git a/bloC/src/main/java/com/iluwatar/bloc/ListenerManager.java b/bloc/src/main/java/com/iluwatar/bloc/ListenerManager.java similarity index 100% rename from bloC/src/main/java/com/iluwatar/bloc/ListenerManager.java rename to bloc/src/main/java/com/iluwatar/bloc/ListenerManager.java diff --git a/bloc/src/main/java/com/iluwatar/bloc/Main.java b/bloc/src/main/java/com/iluwatar/bloc/Main.java new file mode 100644 index 000000000000..1499e6211e56 --- /dev/null +++ b/bloc/src/main/java/com/iluwatar/bloc/Main.java @@ -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(); + } +} \ No newline at end of file diff --git a/bloc/src/main/java/com/iluwatar/bloc/State.java b/bloc/src/main/java/com/iluwatar/bloc/State.java new file mode 100644 index 000000000000..5e184d2af20b --- /dev/null +++ b/bloc/src/main/java/com/iluwatar/bloc/State.java @@ -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) { +} \ No newline at end of file diff --git a/bloC/src/main/java/com/iluwatar/bloc/StateListener.java b/bloc/src/main/java/com/iluwatar/bloc/StateListener.java similarity index 100% rename from bloC/src/main/java/com/iluwatar/bloc/StateListener.java rename to bloc/src/main/java/com/iluwatar/bloc/StateListener.java diff --git a/bloC/src/test/java/com/iluwatar/bloc/BlocTest.java b/bloc/src/test/java/com/iluwatar/bloc/BlocTest.java similarity index 84% rename from bloC/src/test/java/com/iluwatar/bloc/BlocTest.java rename to bloc/src/test/java/com/iluwatar/bloc/BlocTest.java index d3ef70b63ad3..e47d24634a70 100644 --- a/bloC/src/test/java/com/iluwatar/bloc/BlocTest.java +++ b/bloc/src/test/java/com/iluwatar/bloc/BlocTest.java @@ -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"); } @@ -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."); diff --git a/bloC/src/test/java/com/iluwatar/bloc/BlocUiTest.java b/bloc/src/test/java/com/iluwatar/bloc/BlocUiTest.java similarity index 99% rename from bloC/src/test/java/com/iluwatar/bloc/BlocUiTest.java rename to bloc/src/test/java/com/iluwatar/bloc/BlocUiTest.java index 49917523cd93..7b793f89461a 100644 --- a/bloC/src/test/java/com/iluwatar/bloc/BlocUiTest.java +++ b/bloc/src/test/java/com/iluwatar/bloc/BlocUiTest.java @@ -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()); diff --git a/bloC/src/test/java/com/iluwatar/bloc/MainTest.java b/bloc/src/test/java/com/iluwatar/bloc/MainTest.java similarity index 100% rename from bloC/src/test/java/com/iluwatar/bloc/MainTest.java rename to bloc/src/test/java/com/iluwatar/bloc/MainTest.java diff --git a/pom.xml b/pom.xml index 4786d4230974..c20b275168b9 100644 --- a/pom.xml +++ b/pom.xml @@ -218,7 +218,7 @@ function-composition microservices-distributed-tracing microservices-idempotent-consumer - bloC + bloc