diff --git a/monostate/src/main/java/com/iluwatar/monostate/App.java b/monostate/src/main/java/com/iluwatar/monostate/App.java
index a96895126..0daad5b67 100644
--- a/monostate/src/main/java/com/iluwatar/monostate/App.java
+++ b/monostate/src/main/java/com/iluwatar/monostate/App.java
@@ -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.
+ *
+ *
+ * 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.
+ *
+ * .
+ *
+ */
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"));
+ }
}
diff --git a/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java b/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java
index 29a101b81..7bc0043e8 100644
--- a/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java
+++ b/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java
@@ -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 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);
}
+
}
diff --git a/monostate/src/main/java/com/iluwatar/monostate/Request.java b/monostate/src/main/java/com/iluwatar/monostate/Request.java
index c57072a21..037ab96aa 100644
--- a/monostate/src/main/java/com/iluwatar/monostate/Request.java
+++ b/monostate/src/main/java/com/iluwatar/monostate/Request.java
@@ -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;
diff --git a/monostate/src/main/java/com/iluwatar/monostate/Server.java b/monostate/src/main/java/com/iluwatar/monostate/Server.java
index 43be5300b..15973c40d 100644
--- a/monostate/src/main/java/com/iluwatar/monostate/Server.java
+++ b/monostate/src/main/java/com/iluwatar/monostate/Server.java
@@ -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);
}
}
diff --git a/monostate/src/test/java/com/iluwatar/monostate/AppTest.java b/monostate/src/test/java/com/iluwatar/monostate/AppTest.java
new file mode 100644
index 000000000..c5f1f7e92
--- /dev/null
+++ b/monostate/src/test/java/com/iluwatar/monostate/AppTest.java
@@ -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);
+ }
+
+}