Skip to content

Commit c8fd9f3

Browse files
committed
Monostate pattern iluwatar#85
1 parent 9eb64bc commit c8fd9f3

File tree

8 files changed

+180
-0
lines changed

8 files changed

+180
-0
lines changed

monostate/etc/MonoState.ucls

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true">
4+
<class id="1" language="java" name="com.iluwatar.monostate.LoadBalancer" project="monostate"
5+
file="/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="187" width="158" x="100" y="66"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="com.iluwatar.monostate.Server" project="monostate"
14+
file="/monostate/src/main/java/com/iluwatar/monostate/Server.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="176" y="372"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<class id="3" language="java" name="com.iluwatar.monostate.Request" project="monostate"
23+
file="/monostate/src/main/java/com/iluwatar/monostate/Request.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="329" y="352"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<dependency id="4">
32+
<end type="SOURCE" refId="1"/>
33+
<end type="TARGET" refId="3"/>
34+
</dependency>
35+
<dependency id="5">
36+
<end type="SOURCE" refId="2"/>
37+
<end type="TARGET" refId="3"/>
38+
</dependency>
39+
<association id="6">
40+
<end type="SOURCE" refId="1" navigable="false">
41+
<attribute id="7" name="servers"/>
42+
<multiplicity id="8" minimum="0" maximum="2147483647"/>
43+
</end>
44+
<end type="TARGET" refId="2" navigable="true"/>
45+
<display labels="true" multiplicity="true"/>
46+
</association>
47+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
48+
sort-features="false" accessors="true" visibility="true">
49+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
50+
<operations public="true" package="true" protected="true" private="true" static="true"/>
51+
</classifier-display>
52+
<association-display labels="true" multiplicity="true"/>
53+
</class-diagram>

monostate/etc/monostate.png

16.1 KB
Loading

monostate/index.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: pattern
3+
title: MonoState
4+
folder: monostate
5+
permalink: /patterns/monostate/
6+
categories: Creational
7+
tags: Java
8+
---
9+
10+
**Intent:** Enforces a behaviour like sharing the same state amongst all instances.
11+
12+
![alt text](./etc/monostate.png "MonoState")
13+
14+
**Applicability:** Use the Monostate pattern when
15+
16+
* The same state must be shared across all instances of a class.
17+
* Typically this pattern might be used everywhere a SingleTon might be used. Singleton usage however is not transparent, Monostate usage is.
18+
* 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.
19+
20+
**Typical Use Case:**
21+
22+
* the logging class
23+
* managing a connection to a database
24+
* file manager
25+
26+
**Real world examples:**
27+
28+
Yet to see this.

monostate/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.6.0</version>
9+
</parent>
10+
<artifactId>monostate</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar.monostate;
2+
3+
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+
}
10+
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar.monostate;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class LoadBalancer {
7+
private static List<Server> servers = new ArrayList<>();
8+
private static int id = 0;
9+
private static int lastServedId = 0;
10+
11+
static {
12+
servers.add(new Server("localhost", 8081, ++id));
13+
servers.add(new Server("localhost", 8080, ++id));
14+
servers.add(new Server("localhost", 8082, ++id));
15+
servers.add(new Server("localhost", 8083, ++id));
16+
servers.add(new Server("localhost", 8084, ++id));
17+
}
18+
19+
public final void addServer(Server server) {
20+
synchronized (servers) {
21+
servers.add(server);
22+
}
23+
24+
}
25+
26+
public final int getNoOfServers() {
27+
return servers.size();
28+
}
29+
30+
public void serverequest(Request request) {
31+
if (lastServedId >= servers.size()) {
32+
lastServedId = 0;
33+
}
34+
Server server = servers.get(lastServedId++);
35+
server.serve(request);
36+
}
37+
38+
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar.monostate;
2+
3+
public class Request {
4+
public final String value;
5+
6+
public Request(String value) {
7+
super();
8+
this.value = value;
9+
}
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.monostate;
2+
3+
public class Server {
4+
public final String host;
5+
public final int port;
6+
public final int id;
7+
public Server(String host, int port, int id) {
8+
this.host = host;
9+
this.port = port;
10+
this.id = id;
11+
}
12+
public String getHost() {
13+
return host;
14+
}
15+
public int getPort() {
16+
return port;
17+
}
18+
public final void serve(Request request) {
19+
System.out.println("Server ID "+id + " processed request with value "+request.value);
20+
}
21+
}

0 commit comments

Comments
 (0)