Single mode briefly

/ ** 

Singleton: 
New the User (); 
The constructor of a class is set to private, public and outside a private constructor may call the class interface methods 
may only be instantiated out an object, i.e., in the memory there is only a class of objects, called singleton 

application scenario: 
the task Manager window system, window interface services, chat window, adopted singleton pattern. 

* / 


// when a hungry man mode 1. class loading, it will be out of the class object is instantiated, (footprint) called directly to users. 

public  class HungryDemo {
     //
     Private HungryDemo () { 
        
    } 
    
    // static variable modification is a global variable, there is only a change amount of the object in memory. 
    Private  static HungryDemo Hungry = new new HungryDemo (); 
    
    // provides external access method. 
    public  static HungryDemo the getInstance () {
         return Hungry; 
    } 
        

}



/ * A, lazy mode: 
    lazy formula singletons implemented: 
    that is, when the class is loaded, there is no instance of the object class, 
    
    the characteristics of: saving memory space, is relatively inefficient. 
    * / 
    
Public  class StarvingDemo {
     Private StarvingDemo () { 
            
    } 
    
    // give him a null 
    Private  static StarvingDemo S = null ; 
    
    public  static StarvingDemo the getInstance () {
         IF ( null == S) { 
            S = new new StarvingDemo (); 
        } the else { 
            System.out.println ( "memory has had the object." ); 
        }
        
        return s;
    }
    
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

 

Guess you like

Origin www.cnblogs.com/ZXF6/p/11457196.html