提交 c8fd9f3a 编写于 作者: A amit2103

Monostate pattern #85

上级 9eb64bc0
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.monostate.LoadBalancer" project="monostate"
file="/monostate/src/main/java/com/iluwatar/monostate/LoadBalancer.java" binary="false" corner="BOTTOM_RIGHT">
<position height="187" width="158" x="100" y="66"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.monostate.Server" project="monostate"
file="/monostate/src/main/java/com/iluwatar/monostate/Server.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="176" y="372"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.monostate.Request" project="monostate"
file="/monostate/src/main/java/com/iluwatar/monostate/Request.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="329" y="352"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<dependency id="4">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="3"/>
</dependency>
<dependency id="5">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="3"/>
</dependency>
<association id="6">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="7" name="servers"/>
<multiplicity id="8" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>
\ No newline at end of file
---
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.
<?xml version="1.0"?>
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.6.0</version>
</parent>
<artifactId>monostate</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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"));
}
}
package com.iluwatar.monostate;
import java.util.ArrayList;
import java.util.List;
public class LoadBalancer {
private static List<Server> 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);
}
}
package com.iluwatar.monostate;
public class Request {
public final String value;
public Request(String value) {
super();
this.value = value;
}
}
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册