diff --git a/monostate/etc/MonoState.ucls b/monostate/etc/MonoState.ucls new file mode 100644 index 000000000..76b48ad0f --- /dev/null +++ b/monostate/etc/MonoState.ucls @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/monostate/etc/monostate.png b/monostate/etc/monostate.png new file mode 100644 index 000000000..273fa9545 Binary files /dev/null and b/monostate/etc/monostate.png differ diff --git a/monostate/index.md b/monostate/index.md new file mode 100644 index 000000000..369d24ff8 --- /dev/null +++ b/monostate/index.md @@ -0,0 +1,28 @@ +--- +layout: pattern +title: MonoState +folder: monostate +permalink: /patterns/monostate/ +categories: Creational +tags: Java +--- + +**Intent:** Enforces a behaviour like sharing the same state amongst all instances. + +![alt text](./etc/monostate.png "MonoState") + +**Applicability:** Use the Monostate pattern when + +* The same state must be shared across all instances of a class. +* Typically this pattern might be used everywhere a SingleTon might be used. Singleton usage however is not transparent, Monostate usage is. +* Monostate has one major advantage over singleton. The subclasses might decorate the shared state as they wish and hence can provide dynamically different behaviour than the base class. + +**Typical Use Case:** + +* the logging class +* managing a connection to a database +* file manager + +**Real world examples:** + +Yet to see this. diff --git a/monostate/pom.xml b/monostate/pom.xml new file mode 100644 index 000000000..fb40432da --- /dev/null +++ b/monostate/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.6.0 + + monostate + + + junit + junit + test + + + diff --git a/monostate/src/main/java/com/iluwatar/monostate/App.java b/monostate/src/main/java/com/iluwatar/monostate/App.java new file mode 100644 index 000000000..a96895126 --- /dev/null +++ b/monostate/src/main/java/com/iluwatar/monostate/App.java @@ -0,0 +1,11 @@ +package com.iluwatar.monostate; + +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")); + } + +} diff --git a/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java b/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java new file mode 100644 index 000000000..29a101b81 --- /dev/null +++ b/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java @@ -0,0 +1,39 @@ +package com.iluwatar.monostate; + +import java.util.ArrayList; +import java.util.List; + +public class LoadBalancer { + private static List servers = new ArrayList<>(); + private static int id = 0; + private static int lastServedId = 0; + + static { + servers.add(new Server("localhost", 8081, ++id)); + servers.add(new Server("localhost", 8080, ++id)); + servers.add(new Server("localhost", 8082, ++id)); + servers.add(new Server("localhost", 8083, ++id)); + servers.add(new Server("localhost", 8084, ++id)); + } + + public final void addServer(Server server) { + synchronized (servers) { + servers.add(server); + } + + } + + public final int getNoOfServers() { + return servers.size(); + } + + public void serverequest(Request request) { + if (lastServedId >= servers.size()) { + lastServedId = 0; + } + 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 new file mode 100644 index 000000000..c57072a21 --- /dev/null +++ b/monostate/src/main/java/com/iluwatar/monostate/Request.java @@ -0,0 +1,10 @@ +package com.iluwatar.monostate; + +public class Request { + public final String value; + + public Request(String value) { + super(); + this.value = value; + } +} diff --git a/monostate/src/main/java/com/iluwatar/monostate/Server.java b/monostate/src/main/java/com/iluwatar/monostate/Server.java new file mode 100644 index 000000000..43be5300b --- /dev/null +++ b/monostate/src/main/java/com/iluwatar/monostate/Server.java @@ -0,0 +1,21 @@ +package com.iluwatar.monostate; + +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); + } +}