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 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Intent:** Enforces a behaviour like sharing the same state amongst all instances.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Applicability:** Use the Monostate pattern when
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Typical Use Case:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  the logging class
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  managing a connection to a database
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  file manager
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Real world examples:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Yet to see this.