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:
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 21:09:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 -  Instantiation
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:06:11 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-07-26 20:33:43 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Also known as
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Borg
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Intent
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Enforces a behaviour like sharing the same state amongst all instances.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:06:11 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-07 20:01:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Class diagram
  
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:06:11 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## 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. 
						 
					
						
							
								
									
										
										
										
											2015-10-09 23:20:58 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  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. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Typical Use Case
  
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:06:11 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 21:09:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								*  The logging class 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  Managing a connection to a database 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*  File manager 
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:06:11 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Real world examples
  
						 
					
						
							
								
									
										
										
										
											2015-10-04 03:06:11 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Yet to see this.