Java-code block, singleton design pattern

Code block

A piece of code written in the class is similar to the function of the constructor. Each time an object is created, the code block is automatically called

public  class Test1 {
     // Modify any variable or method with static: there is a separate storage space in memory, the outside world can directly access 
    public  static  void main (String [] args) by class name { 
        Doo doo1 = new Doo (); 
        System.out.println (); 
        Doo doo2 = new Doo (); 
    } 
} 
class Doo { 
    { // code block, every time a new object is created, this part of 
        System.out.println ("code block is called by default " ); 
    } 
}

1) The construction method in the enterprise and completely replace the code block
2) The static modified code block is called a static code block. No matter how many objects are created, the
static code block is often used in enterprise development to perform some initialization operations.

public class Test1 {
    public static void main(String[] args) {
        Boo boo1=new Boo();
        Boo boo2=new Boo();
        Boo boo3=new Boo();
    }
}
class Boo{
    static{
        System.out.println("静态代码块");
    }
}

Singleton design pattern

1. Design mode: The idea of ​​programmers writing code summary, the design rules of the most commonly used codes in software development
2. Single-case design mode: to ensure that a class can only have one object in a system, it must be remembered that the simplest A design pattern
3. The benefits of singletons: control the number of objects of this type, there can be only one object, reduce system pressure, improve performance
4. Hungry singletons (emphasis):

public  class Test1 {
     public  static  void main (String [] args) { 
        Moo moo1 = Moo.getInstance (); 
        Moo moo2 = Moo.getInstance ();
 // If two objects are the same object, use "==" the result is a return to true 
        System.out.println (Moo1 == MoO2); // result is to true 
    } 
} 
class Moo {
     private Moo () {} // constructor must be private: the outside world can not just use the constructor to create the object 
    private  static Moo moo = new Moo (); // Use static attributes to save objects of this class (modify attributes or methods with static, there is only one object in memory) 
    public  static Moo getInstance () {// Provide a static method to get the object 
        return moo; 
    } 
}

5. Lazy-style singleton (better to write): The
construction method is private, the object in memory is not created directly, but by way of judgment, if there is an object of this type, it will not be created, if not, a new one will be created Object

public  class Test1 {
     public  static  void main (String [] args) {
 // The getInstance method is executed for the first time, no Noo object will be created, a Noo object will be created and returned 
        Noo noo1 = Noo.getInstance ();
 // The second time Execute the getInstance method, the Noo object already exists in the memory, directly return the Noo object, no longer create a new object 
        Noo noo2 = Noo.getInstance (); 
    } 
} 
class Noo {
     private Noo () {} // Construction method private
 // Define the object (static) 
    private  static Noo noo = null ; // Don't create an object of this class first, just define it first
 // get the method of the object (judgment) 
    public  staticNoo getInstance () { // Provide a method to obtain the object of this class (in this method to judge, if not, create the object, if not, create the object of this class) 
        if (noo == null ) { 
            noo = new Noo (); 
        } 
        return noo; 
    } 
}

Finish

Guess you like

Origin www.cnblogs.com/peiya/p/12691324.html