Added Comments and Feedback
This commit is contained in:
parent
1e988c10f9
commit
19f5966cb9
@ -1,11 +1,35 @@
|
|||||||
package com.iluwatar.monostate;
|
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.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* In the following example, The {@link LoadBalancer} class represents the app's logic. It contains
|
||||||
|
* a series of Servers, which can handle requests of type {@link Request}. Two instances of
|
||||||
|
* LoadBalacer are created. When a request is made to a server via the first LoadBalancer the state
|
||||||
|
* change in the first load balancer affects the second. So if the first LoadBalancer selects the
|
||||||
|
* Server 1, the second LoadBalancer on a new request will select the Second server. If a third
|
||||||
|
* LoadBalancer is created and a new request is made to it, then it will select the third server as
|
||||||
|
* the second load balancer has already selected the second server.
|
||||||
|
* <p>
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
LoadBalancer loadBalancer1 = new LoadBalancer();
|
* Program entry point
|
||||||
LoadBalancer loadBalancer2 = new LoadBalancer();
|
*
|
||||||
loadBalancer1.serverequest(new Request("Hello"));
|
* @param args command line args
|
||||||
loadBalancer2.serverequest(new Request("Hello World"));
|
*/
|
||||||
}
|
public static void main(String[] args) {
|
||||||
|
LoadBalancer loadBalancer1 = new LoadBalancer();
|
||||||
|
LoadBalancer loadBalancer2 = new LoadBalancer();
|
||||||
|
loadBalancer1.serverequest(new Request("Hello"));
|
||||||
|
loadBalancer2.serverequest(new Request("Hello World"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,14 @@ package com.iluwatar.monostate;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The LoadBalancer class. This implements the MonoState pattern. It holds a series of servers. Upon
|
||||||
|
* receiving a new Request, it delegates the call to the servers in a Round Robin Fashion. Since all
|
||||||
|
* instances of the class share the same state, all instances will delegate to the same server on
|
||||||
|
* receiving a new Request.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
public class LoadBalancer {
|
public class LoadBalancer {
|
||||||
private static List<Server> servers = new ArrayList<>();
|
private static List<Server> servers = new ArrayList<>();
|
||||||
private static int id = 0;
|
private static int id = 0;
|
||||||
@ -27,6 +35,10 @@ public class LoadBalancer {
|
|||||||
return servers.size();
|
return servers.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int getLastServedId() {
|
||||||
|
return lastServedId;
|
||||||
|
}
|
||||||
|
|
||||||
public void serverequest(Request request) {
|
public void serverequest(Request request) {
|
||||||
if (lastServedId >= servers.size()) {
|
if (lastServedId >= servers.size()) {
|
||||||
lastServedId = 0;
|
lastServedId = 0;
|
||||||
@ -34,6 +46,7 @@ public class LoadBalancer {
|
|||||||
Server server = servers.get(lastServedId++);
|
Server server = servers.get(lastServedId++);
|
||||||
server.serve(request);
|
server.serve(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
package com.iluwatar.monostate;
|
package com.iluwatar.monostate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* The Request class. A {@link Server} can handle an instance of a Request.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
public class Request {
|
public class Request {
|
||||||
public final String value;
|
public final String value;
|
||||||
|
|
||||||
|
@ -1,21 +1,31 @@
|
|||||||
package com.iluwatar.monostate;
|
package com.iluwatar.monostate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* The Server class. Each Server sits behind a LoadBalancer which delegates the call to the
|
||||||
|
* servers in a simplistic Round Robin fashion.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class Server {
|
public class Server {
|
||||||
public final String host;
|
public final String host;
|
||||||
public final int port;
|
public final int port;
|
||||||
public final int id;
|
public final int id;
|
||||||
|
|
||||||
public Server(String host, int port, int id) {
|
public Server(String host, int port, int id) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void serve(Request request) {
|
public final void serve(Request request) {
|
||||||
System.out.println("Server ID "+id + " processed request with value "+request.value);
|
System.out.println("Server ID " + id + " processed request with value " + request.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
monostate/src/test/java/com/iluwatar/monostate/AppTest.java
Normal file
26
monostate/src/test/java/com/iluwatar/monostate/AppTest.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package com.iluwatar.monostate;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class AppTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSameStateAmonstAllInstances() {
|
||||||
|
LoadBalancer balancer = new LoadBalancer();
|
||||||
|
LoadBalancer balancer2 = new LoadBalancer();
|
||||||
|
balancer.addServer(new Server("localhost", 8085, 6));
|
||||||
|
// Both should have the same number of servers.
|
||||||
|
Assert.assertTrue(balancer.getNoOfServers() == balancer2.getNoOfServers());
|
||||||
|
// Both Should have the same LastServedId
|
||||||
|
Assert.assertTrue(balancer.getLastServedId() == balancer2.getLastServedId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMain() {
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
Assert.assertTrue(LoadBalancer.getLastServedId() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user