mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-19 16:23:51 +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:
@@ -24,7 +24,6 @@
|
||||
*/
|
||||
package com.iluwatar.monostate;
|
||||
|
||||
|
||||
/**
|
||||
* The MonoState pattern ensures that all instances of the class will have the same state. This can
|
||||
* be used a direct replacement of the Singleton pattern.
|
||||
@@ -49,5 +48,4 @@ public class App {
|
||||
loadBalancer1.serverRequest(new Request("Hello"));
|
||||
loadBalancer2.serverRequest(new Request("Hello World"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,26 +33,22 @@ import java.util.List;
|
||||
* instances of the class share the same state, all instances will delegate to the same server on
|
||||
* receiving a new Request.
|
||||
*/
|
||||
|
||||
public class LoadBalancer {
|
||||
private static final List<Server> SERVERS = new ArrayList<>();
|
||||
private static int lastServedId;
|
||||
|
||||
static {
|
||||
var id = 0;
|
||||
for (var port : new int[]{8080, 8081, 8082, 8083, 8084}) {
|
||||
for (var port : new int[] {8080, 8081, 8082, 8083, 8084}) {
|
||||
SERVERS.add(new Server("localhost", port, ++id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new server.
|
||||
*/
|
||||
/** Add new server. */
|
||||
public final void addServer(Server server) {
|
||||
synchronized (SERVERS) {
|
||||
SERVERS.add(server);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final int getNoOfServers() {
|
||||
@@ -63,9 +59,7 @@ public class LoadBalancer {
|
||||
return lastServedId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle request.
|
||||
*/
|
||||
/** Handle request. */
|
||||
public synchronized void serverRequest(Request request) {
|
||||
if (lastServedId >= SERVERS.size()) {
|
||||
lastServedId = 0;
|
||||
@@ -73,5 +67,4 @@ public class LoadBalancer {
|
||||
var server = SERVERS.get(lastServedId++);
|
||||
server.serve(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,8 +24,5 @@
|
||||
*/
|
||||
package com.iluwatar.monostate;
|
||||
|
||||
/**
|
||||
* The Request record. A {@link Server} can handle an instance of a Request.
|
||||
*/
|
||||
|
||||
/** The Request record. A {@link Server} can handle an instance of a Request. */
|
||||
public record Request(String value) {}
|
||||
|
||||
@@ -39,9 +39,7 @@ public class Server {
|
||||
public final int port;
|
||||
public final int id;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public Server(String host, int port, int id) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
@@ -49,7 +47,11 @@ public class Server {
|
||||
}
|
||||
|
||||
public void serve(Request request) {
|
||||
LOGGER.info("Server ID {} associated to host : {} and port {}. Processed request with value {}",
|
||||
id, host, port, request.value());
|
||||
LOGGER.info(
|
||||
"Server ID {} associated to host : {} and port {}. Processed request with value {}",
|
||||
id,
|
||||
host,
|
||||
port,
|
||||
request.value());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application Test Entry
|
||||
*/
|
||||
|
||||
/** Application Test Entry */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,10 +35,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* LoadBalancerTest
|
||||
*
|
||||
*/
|
||||
/** LoadBalancerTest */
|
||||
class LoadBalancerTest {
|
||||
|
||||
@Test
|
||||
@@ -71,7 +68,5 @@ class LoadBalancerTest {
|
||||
|
||||
verify(server, times(2)).serve(request);
|
||||
verifyNoMoreInteractions(server);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user