Added Comments and Feedback
This commit is contained in:
parent
1e988c10f9
commit
19f5966cb9
@ -1,11 +1,35 @@
|
||||
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 static void main(String[] args) {
|
||||
LoadBalancer loadBalancer1 = new LoadBalancer();
|
||||
LoadBalancer loadBalancer2 = new LoadBalancer();
|
||||
loadBalancer1.serverequest(new Request("Hello"));
|
||||
loadBalancer2.serverequest(new Request("Hello World"));
|
||||
}
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
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.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 {
|
||||
private static List<Server> servers = new ArrayList<>();
|
||||
private static int id = 0;
|
||||
@ -27,6 +35,10 @@ public class LoadBalancer {
|
||||
return servers.size();
|
||||
}
|
||||
|
||||
public static int getLastServedId() {
|
||||
return lastServedId;
|
||||
}
|
||||
|
||||
public void serverequest(Request request) {
|
||||
if (lastServedId >= servers.size()) {
|
||||
lastServedId = 0;
|
||||
@ -34,6 +46,7 @@ public class LoadBalancer {
|
||||
Server server = servers.get(lastServedId++);
|
||||
server.serve(request);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,11 @@
|
||||
package com.iluwatar.monostate;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Request class. A {@link Server} can handle an instance of a Request.
|
||||
*
|
||||
*/
|
||||
|
||||
public class Request {
|
||||
public final String value;
|
||||
|
||||
|
@ -1,21 +1,31 @@
|
||||
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 final String host;
|
||||
public final int port;
|
||||
public final int id;
|
||||
|
||||
public Server(String host, int port, int id) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
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