Skip to content

Commit 19f5966

Browse files
committed
Added Comments and Feedback
1 parent 1e988c1 commit 19f5966

File tree

5 files changed

+86
-7
lines changed

5 files changed

+86
-7
lines changed
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
package com.iluwatar.monostate;
22

3+
4+
5+
/**
6+
*
7+
* The MonoState pattern ensures that all instances of the class will have the same state. This can
8+
* be used a direct replacement of the Singleton pattern.
9+
*
10+
* <p>
11+
* In the following example, The {@link LoadBalancer} class represents the app's logic. It contains
12+
* a series of Servers, which can handle requests of type {@link Request}. Two instances of
13+
* LoadBalacer are created. When a request is made to a server via the first LoadBalancer the state
14+
* change in the first load balancer affects the second. So if the first LoadBalancer selects the
15+
* Server 1, the second LoadBalancer on a new request will select the Second server. If a third
16+
* LoadBalancer is created and a new request is made to it, then it will select the third server as
17+
* the second load balancer has already selected the second server.
18+
* <p>
19+
* .
20+
*
21+
*/
322
public class App {
4-
public static void main(String[] args) {
5-
LoadBalancer loadBalancer1 = new LoadBalancer();
6-
LoadBalancer loadBalancer2 = new LoadBalancer();
7-
loadBalancer1.serverequest(new Request("Hello"));
8-
loadBalancer2.serverequest(new Request("Hello World"));
9-
}
23+
/**
24+
* Program entry point
25+
*
26+
* @param args command line args
27+
*/
28+
public static void main(String[] args) {
29+
LoadBalancer loadBalancer1 = new LoadBalancer();
30+
LoadBalancer loadBalancer2 = new LoadBalancer();
31+
loadBalancer1.serverequest(new Request("Hello"));
32+
loadBalancer2.serverequest(new Request("Hello World"));
33+
}
1034

1135
}

monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
/**
7+
* The LoadBalancer class. This implements the MonoState pattern. It holds a series of servers. Upon
8+
* receiving a new Request, it delegates the call to the servers in a Round Robin Fashion. Since all
9+
* instances of the class share the same state, all instances will delegate to the same server on
10+
* receiving a new Request.
11+
*
12+
*/
13+
614
public class LoadBalancer {
715
private static List<Server> servers = new ArrayList<>();
816
private static int id = 0;
@@ -27,13 +35,18 @@ public final int getNoOfServers() {
2735
return servers.size();
2836
}
2937

38+
public static int getLastServedId() {
39+
return lastServedId;
40+
}
41+
3042
public void serverequest(Request request) {
3143
if (lastServedId >= servers.size()) {
3244
lastServedId = 0;
3345
}
3446
Server server = servers.get(lastServedId++);
3547
server.serve(request);
3648
}
49+
3750

3851

3952
}

monostate/src/main/java/com/iluwatar/monostate/Request.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.iluwatar.monostate;
22

3+
/**
4+
*
5+
* The Request class. A {@link Server} can handle an instance of a Request.
6+
*
7+
*/
8+
39
public class Request {
410
public final String value;
511

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
package com.iluwatar.monostate;
22

3+
/**
4+
*
5+
* The Server class. Each Server sits behind a LoadBalancer which delegates the call to the
6+
* servers in a simplistic Round Robin fashion.
7+
*
8+
*/
39
public class Server {
410
public final String host;
511
public final int port;
612
public final int id;
13+
714
public Server(String host, int port, int id) {
815
this.host = host;
916
this.port = port;
1017
this.id = id;
1118
}
19+
1220
public String getHost() {
1321
return host;
1422
}
23+
1524
public int getPort() {
1625
return port;
1726
}
27+
1828
public final void serve(Request request) {
19-
System.out.println("Server ID "+id + " processed request with value "+request.value);
29+
System.out.println("Server ID " + id + " processed request with value " + request.value);
2030
}
2131
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.iluwatar.monostate;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class AppTest {
7+
8+
@Test
9+
public void testSameStateAmonstAllInstances() {
10+
LoadBalancer balancer = new LoadBalancer();
11+
LoadBalancer balancer2 = new LoadBalancer();
12+
balancer.addServer(new Server("localhost", 8085, 6));
13+
// Both should have the same number of servers.
14+
Assert.assertTrue(balancer.getNoOfServers() == balancer2.getNoOfServers());
15+
// Both Should have the same LastServedId
16+
Assert.assertTrue(balancer.getLastServedId() == balancer2.getLastServedId());
17+
}
18+
19+
@Test
20+
public void testMain() {
21+
String[] args = {};
22+
App.main(args);
23+
Assert.assertTrue(LoadBalancer.getLastServedId() == 2);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)