Java learning grammar day18- Singleton (Singleton) design pattern and understand the main methods

First, what is the singleton design pattern

  Design model is summed up in a lot of practice and theory of the preferred structure of the code, programming style, as well as problem-solving way of thinking. In short, in the actual programming, summed up some of the problem-solving routines.

  It refers to a single embodiment only one instance.

  The Singleton design pattern called class, is to take a certain way to ensure the entire software system, there is only a certain class of object instance, and the class is only a method to obtain its object instance. If we want class in a virtual machine can only produce an object, we must first access the class constructor is set to private, so, you can not use the new operator class objects generated outside of class, but in internal class still produce objects of that class. Since the beginning of object classes can not get outside the class can only be called a static method of the class to return the object class internally created, static methods can only access static member variables in the class, so the class point internally generated variables that object must also be defined as static.

Second, the singleton design pattern - starving formula

Package Day14;
 / ** 
 * singleton starving formula 
 * 
 * * / 
public  class Single {
 //     public Single () {
 //         // assumed to be configured to perform line 1000, to take up a lot of resources, consumption long (10s) when
 //         // every object needs new single 10s, 1000 lines of code to run
 //         // like this, it is suitable for single mode embodiment, only one new object after the object has been on the use
 / /     } 
    
    
    // do first private constructor, privatization constructor calls this class man can be used directly to create new objects 
    private Single () { 
        
        
    } 
    
    // class variable private Single type 
    private  static Single SINGLE = new Single (); //single private class variables, only one in the whole code to run, so the new Single () only new once, the object is assigned to a single 
    
    public  static Single getInstance () { // How many times getInstance method regardless of the call, returns to Object single 
        return single; 
    } 
}
Package Day14; 

public  class the Test {
     public  static  void main (String [] args) { 
        Single S = Single.getInstance (); 
        Single S1 = Single.getInstance (); 
        Single S2 = Single.getInstance (); 
        Single S3 = Single. getInstance (); 
        Single S4 = Single.getInstance (); // how many times getInstance methods regardless of the call, returns an object to the single 
        
    } 
}

Third, the singleton design pattern - lazy man.

  In the beginning, the object is null, until the first person to call me, just a new target, after all the calls I have with this object.

Package Day14;
 / ** 
 * lazy formula Singleton 
 * 
 * * / 
public  class Single1 {
     // 1. First privatization construction method, so that external objects can not be directly new 
    Private Single1 () { 
        
    } 
    
    // 2. declare classes rEFERENCE 
    Private  static Single1 S1 = null ; // here need to meet static method, with reference to such a modified static 
    
    // 3. set a common way to access the instance of the class 
    public  static Single1 the getInstance () {
         IF (S1 == null ) { 
            S1 = new new Single1 (); // 3.1 if the class is not created, the first created and then returned to the caller: this class.
        } 
        
        Return S1; // 3.2 If an instance of the class, returns directly to the caller. 
    } 
}
Package Day14; 

public  class the Test {
     public  static  void main (String [] args) { 
                Single1 S = Single1.getInstance (); // S point S1, s1 originally null, the first call getInstance () method after, s1 becomes Single1 become new new () 
        Single1 Single1.getInstance S1 = (); // but also point s1-s4 s1, s1 at this time has become a new Single () objects, if the statement is not executed, direct return s1. 
        S2 = Single1 Single1.getInstance (); 
        Single1 S3 = Single1.getInstance (); 
        Single1 S4 = Single1.getInstance (); 
        
    } 
}

Summary: singleton mode, the software is running one and only one instance of the object, which means only new once.

  The difference between the lazy man and a hungry man is when new type of object:

  Lazy style, to the new object is the first time someone calls getInstance () method, and then later someone calls getInstance () method returns directly to the first new good object.

  Hungry Chinese-style, after the class is loaded, no one has time to call it a good first new objects, regardless of the future who will call the getInstance () method, new good before that object is returned directly.

 

Fourth, understand the syntax of the main method

  public static void main(String[] args){}

  Since Java virtual machine needs to main () method calls the class, so the access method must be public. And because the Java virtual machine does not have to create objects in the implementation of main () method, this method must be static, and can be called directly by the class name. Method. Because there is no return value, so use void. The main method name. (String [] args) is a parameter, the method receives a main array of type String parameter, operating parameters passed to the class stored in the array Java execution command.

  Use cmd to run to understand:

public class TestMain {
    public static void main(String[] args){
        for(int i = 0; i<args.length; i++){
            System.out.println(args[i]);
        }
    }
}

Run into cmd and add parameters, the result is:

 

Guess you like

Origin www.cnblogs.com/su-peng/p/12516237.html