File tree Expand file tree Collapse file tree 5 files changed +86
-7
lines changed
main/java/com/iluwatar/monostate
test/java/com/iluwatar/monostate Expand file tree Collapse file tree 5 files changed +86
-7
lines changed Original file line number Diff line number Diff line change 11package 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+ */
322public 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}
Original file line number Diff line number Diff line change 33import java .util .ArrayList ;
44import 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+
614public 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}
Original file line number Diff line number Diff line change 11package com .iluwatar .monostate ;
22
3+ /**
4+ *
5+ * The Request class. A {@link Server} can handle an instance of a Request.
6+ *
7+ */
8+
39public class Request {
410 public final String value ;
511
Original file line number Diff line number Diff line change 11package 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+ */
39public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments