36 lines
883 B
Markdown
Raw Normal View History

2015-10-04 03:06:11 +05:30
---
layout: pattern
title: MonoState
folder: monostate
permalink: /patterns/monostate/
categories: Creational
2015-12-28 15:52:44 +02:00
tags:
- Java
- Difficulty-Beginner
2015-10-04 03:06:11 +05:30
---
## Also known as
Borg
## Intent
Enforces a behaviour like sharing the same state amongst all instances.
2015-10-04 03:06:11 +05:30
![alt text](./etc/monostate.png "MonoState")
## Applicability
Use the Monostate pattern when
2015-10-04 03:06:11 +05:30
* 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.
2015-10-04 03:06:11 +05:30
* 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
2015-10-04 03:06:11 +05:30
* the logging class
* managing a connection to a database
* file manager
## Real world examples
2015-10-04 03:06:11 +05:30
Yet to see this.