37 lines
899 B
Markdown
Raw Permalink Normal View History

2015-10-04 03:06:11 +05:30
---
layout: pattern
title: MonoState
folder: monostate
permalink: /patterns/monostate/
categories: Creational
language: en
2015-12-28 15:52:44 +02:00
tags:
- Instantiation
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
## Class diagram
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
2015-10-04 03:06:11 +05:30
## Real world examples
2015-10-04 03:06:11 +05:30
Yet to see this.