Talk design pattern (B): Example Single (the Singleton) mode

1 Introduction

Actual business, most business class need only one object can be done all the work, and then create other objects it is a waste of memory space. Then we use the singleton pattern, it its name, as this model allows a class can only generate a unique object. The singleton pattern, according to this unique opportunity to create a singleton different, can be divided into lazy mode and a hungry man mode.

2. starving (singleton) mode

 

HungrySingleton class {public 
     // declare a variable current class is the class of variables which allow access to static methods, to ensure that there is a current class of objects 
	Private static LazySingleton singleInstance new new LazySingleton = (); 
	Private String Description; // other attributes 

	/ * 
	 * privatization unique default constructor, it can not create an instance constructor by external (other category) 
	 * / 
	Private LazySingleton () {}   

	/ ** 
	 * returns the unique instance of an object 
	 * @return 
	 * / 
	/ * 
	 * due to constructor is privatized, external can not directly get an instance of an object, you can not call an instance method to obtain a reference object 
	 * (which also caught in an infinite loop. external initially did not hold a reference singleInstance, the practical needs and want to pass this object to call a method to obtain a reference to it, 
	 * if additional external references would have to hold it, you never have to call the method to get a reference to it.) 
	 * can only declare this method as static methods with an object-independent, and static methods can not directly access member variables, singleInstance can only be declared as static class variables 
	 * / 
	public LazySingleton the getInstance static () {  
		return singleInstance;
	} 

}

 

3. lazy (singleton) mode

 

LazySingleton class {public 
	// use "volatile" keyword statement, compiler optimization to prevent change memory allocation order 
	Private volatile static LazySingleton singleInstance = null; 
	// classifier lock 
	Private Class Final static LazySingleton.class CLASS_LOCK = <?>; 
	
	// object counters 
	Private instanceCount static int = 0; 

	Private LazySingleton () { 
	} 

	/ ** 
	 * returns the class object is unique 
	 * @return 
	 * / 
	public static LazySingleton the getInstance () { 
		/ * 
		 * sync blocks disposed, in multi-threaded to ensure that the conditions There is a single object (a single thread, synchronization is not required) 
		 * 
		 * not only detects singleInstance instantiated, it is provided sync block. 
		 * If it is detected singleInstance has been instantiated, the process directly returns its reference. 
		 * This double lock synchronization verification, the entire process than the set code sync block or a method declared synchronization method, the performance is much higher. 
		 * Because dual synchronization verification, synchronization only once, which additionally has two ideas need to be synchronized.
		 * 
		 */
		if (singleInstance == null) {
			synchronized (CLASS_LOCK) {
				if (singleInstance == null) {
					singleInstance = new LazySingleton();
					instanceCount++;
				}
			}
		}
		return singleInstance;
	}
}

  

 

 

Guess you like

Origin www.cnblogs.com/gocode/p/singleton-pattern.html