mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-11 00:25:02 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -30,17 +30,15 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* The Bloc class is responsible for managing the current state and notifying registered listeners
|
||||
* whenever the state changes. It implements the ListenerManager interface, allowing listeners
|
||||
* to be added, removed, and notified of state changes.
|
||||
* whenever the state changes. It implements the ListenerManager interface, allowing listeners to be
|
||||
* added, removed, and notified of state changes.
|
||||
*/
|
||||
public class Bloc implements ListenerManager<State> {
|
||||
|
||||
private State currentState;
|
||||
private final List<StateListener<State>> listeners = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructs a new Bloc instance with an initial state of value 0.
|
||||
*/
|
||||
/** Constructs a new Bloc instance with an initial state of value 0. */
|
||||
public Bloc() {
|
||||
this.currentState = new State(0);
|
||||
}
|
||||
@@ -88,16 +86,12 @@ public class Bloc implements ListenerManager<State> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current state value by 1 and notifies listeners of the change.
|
||||
*/
|
||||
/** Increments the current state value by 1 and notifies listeners of the change. */
|
||||
public void increment() {
|
||||
emitState(new State(currentState.value() + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the current state value by 1 and notifies listeners of the change.
|
||||
*/
|
||||
/** Decrements the current state value by 1 and notifies listeners of the change. */
|
||||
public void decrement() {
|
||||
emitState(new State(currentState.value() - 1));
|
||||
}
|
||||
|
||||
@@ -32,14 +32,10 @@ import javax.swing.JLabel;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
/**
|
||||
* The BlocUI class handles the creation and management of the UI components.
|
||||
*/
|
||||
/** The BlocUI class handles the creation and management of the UI components. */
|
||||
public class BlocUi {
|
||||
|
||||
/**
|
||||
* Creates and shows the UI.
|
||||
*/
|
||||
/** Creates and shows the UI. */
|
||||
public void createAndShowUi() {
|
||||
// Create a Bloc instance to manage the state
|
||||
final Bloc bloc = new Bloc();
|
||||
@@ -70,19 +66,20 @@ public class BlocUi {
|
||||
// adding the listener to the Bloc instance
|
||||
bloc.addListener(stateListener);
|
||||
|
||||
toggleListenerButton.addActionListener(e -> {
|
||||
if (bloc.getListeners().contains(stateListener)) {
|
||||
bloc.removeListener(stateListener);
|
||||
toggleListenerButton.setText("Enable Listener");
|
||||
} else {
|
||||
bloc.addListener(stateListener);
|
||||
toggleListenerButton.setText("Disable Listener");
|
||||
}
|
||||
});
|
||||
toggleListenerButton.addActionListener(
|
||||
e -> {
|
||||
if (bloc.getListeners().contains(stateListener)) {
|
||||
bloc.removeListener(stateListener);
|
||||
toggleListenerButton.setText("Enable Listener");
|
||||
} else {
|
||||
bloc.addListener(stateListener);
|
||||
toggleListenerButton.setText("Disable Listener");
|
||||
}
|
||||
});
|
||||
|
||||
incrementButton.addActionListener(e -> bloc.increment());
|
||||
decrementButton.addActionListener(e -> bloc.decrement());
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bloc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -30,7 +31,6 @@ import java.util.List;
|
||||
*
|
||||
* @param <T> The type of state to be handled by the listeners.
|
||||
*/
|
||||
|
||||
public interface ListenerManager<T> {
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,19 +25,18 @@
|
||||
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.
|
||||
* 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 {
|
||||
|
||||
/**
|
||||
@@ -49,4 +48,4 @@ public class Main {
|
||||
BlocUi blocUi = new BlocUi();
|
||||
blocUi.createAndShowUi();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@
|
||||
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.
|
||||
* 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) {
|
||||
}
|
||||
public record State(int value) {}
|
||||
|
||||
@@ -26,7 +26,8 @@ package com.iluwatar.bloc;
|
||||
|
||||
/**
|
||||
* The {@code StateListener} interface defines the contract for listening to state changes.
|
||||
* Implementations of this interface should handle state changes and define actions to take when the state changes.
|
||||
* Implementations of this interface should handle state changes and define actions to take when the
|
||||
* state changes.
|
||||
*
|
||||
* @param <T> the type of state that this listener will handle
|
||||
*/
|
||||
|
||||
@@ -23,19 +23,23 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bloc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BlocTest {
|
||||
private Bloc bloc;
|
||||
private AtomicInteger stateValue;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
bloc = new Bloc();
|
||||
stateValue = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialState() {
|
||||
assertTrue(bloc.getListeners().isEmpty(), "No listeners should be present initially.");
|
||||
@@ -68,6 +72,7 @@ class BlocTest {
|
||||
bloc.removeListener(listener);
|
||||
assertTrue(bloc.getListeners().isEmpty(), "Listener count should be 0 after removal.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipleListeners() {
|
||||
AtomicInteger secondValue = new AtomicInteger();
|
||||
@@ -77,4 +82,4 @@ class BlocTest {
|
||||
assertEquals(1, stateValue.get(), "First listener should receive state 1.");
|
||||
assertEquals(1, secondValue.get(), "Second listener should receive state 1.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.bloc;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import javax.swing.*;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BlocUiTest {
|
||||
|
||||
@@ -43,9 +42,9 @@ public class BlocUiTest {
|
||||
private Bloc bloc;
|
||||
private StateListener<State> stateListener;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
bloc = new Bloc(); // Re-initialize the Bloc for each test
|
||||
bloc = new Bloc(); // Re-initialize the Bloc for each test
|
||||
|
||||
frame = new JFrame("BloC example");
|
||||
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
@@ -69,26 +68,26 @@ public class BlocUiTest {
|
||||
|
||||
incrementButton.addActionListener(e -> bloc.increment());
|
||||
decrementButton.addActionListener(e -> bloc.decrement());
|
||||
toggleListenerButton.addActionListener(e -> {
|
||||
if (bloc.getListeners().contains(stateListener)) {
|
||||
bloc.removeListener(stateListener);
|
||||
toggleListenerButton.setText("Enable Listener");
|
||||
} else {
|
||||
bloc.addListener(stateListener);
|
||||
toggleListenerButton.setText("Disable Listener");
|
||||
}
|
||||
});
|
||||
toggleListenerButton.addActionListener(
|
||||
e -> {
|
||||
if (bloc.getListeners().contains(stateListener)) {
|
||||
bloc.removeListener(stateListener);
|
||||
toggleListenerButton.setText("Enable Listener");
|
||||
} else {
|
||||
bloc.addListener(stateListener);
|
||||
toggleListenerButton.setText("Disable Listener");
|
||||
}
|
||||
});
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
frame.dispose();
|
||||
bloc = new Bloc(); // Reset Bloc state after each test to avoid state carryover
|
||||
bloc = new Bloc(); // Reset Bloc state after each test to avoid state carryover
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIncrementButton() {
|
||||
simulateButtonClick(incrementButton);
|
||||
|
||||
Reference in New Issue
Block a user