java architecture Road - (design patterns) to create schema five kinds of singleton

  Design pattern itself has not been very understanding, but in fact we have all the time in the use of these design patterns, java has 23 design patterns and six principles.

Design patterns are set to be repeated use, known to most people, after cataloging, code design experience summary. Use design patterns to reusable code, make the code easier to understand others, to ensure the reusability of code reliability program.

Contains

Create a schema, a total of five categories: singleton , factory method pattern, abstract factory pattern, the builder pattern, prototype model.

Structural model, a total of seven kinds: adapter mode, decorative mode, proxy mode, the appearance mode, bridge mode, combination mode, Flyweight.

Behavioral patterns, a total of eleven kinds: Strategy pattern, the template method pattern, observer mode, iterator pattern, the responsibility chain mode, command mode, the memo mode state mode, the visitor pattern, intermediary model to explain the mode.

We are concerned that our creational pattern today

Singleton : is one of the easiest in Java design patterns. It provides the best way to create objects.

This model involves a single class that is responsible for creating your own objects, while ensuring that only a single object is created. This class provides the only way to access the object can directly access, no instance of the object class.

To remember one other way with it, Singleton pattern like we eat potatoes, only one potato, potatoes you call him, that he may also be the potato, also called Potato, but eventually that a potato.

note:

1, only one singleton class instance.

2, singleton class must create their own unique instance.

3, singleton class must provide this example to all other objects.

Code:

  Example starving single mode:

Package Single; 

public  class CarBean { 

    public  static CarBean CarBean = new new CarBean (); 

    Private String name; 

    public String getName () {
         return name; 
    } 

    public  void the setName (String name) {
         the this .name = name; 
    } 

    // closed configuration New method for preventing the subject. 
    Private CarBean () { 
        System.out.println ( "go only once CarBean constructor" ); 
    } 

    public  static CarBean the getInstance () {
         return CarBean; 
    } 

}
Package Single; 

public  class MainTest {
     / ** 
     * Static constant starving single embodiment mode 
     * advantages: The wording in the JVM loads class when it is instantiated, the thread avoids synchronization problems 
     * Disadvantages: loading the class when it is instantiated, the effect did not reach the delay load, resulting in wasted memory 
     * @param args
      * / 
    public  static  void main (String [] args) { 
        CarBean CAR1 = CarBean.getInstance (); 
        CarBean CAR2 = CarBean.getInstance () ; 
        car1.setName ( "Lamborghini" ); 
        car2.setName ( "Maserati" ); 
        System.out.println (car1.getName ()); 
        System.out.println (car2.getName ());
    }
}

  Example lazy single mode (asynchronous):

Package Single2,; 

public  class CarBean { 

    public  static CarBean CarBean = null ; 

    Private String name; 

    public String getName () {
         return name; 
    } 

    public  void the setName (String name) {
         the this .name = name; 
    } 

    // closed configuration preventing method. New objects 
    Private CarBean () { 
        System.out.println ( "go only once CarBean constructor" ); 
    } 

    public  static CarBean the getInstance () {
         IF (CarBean == null) {
            carBean = new CarBean();
        }
        return carBean;
    }

}
Package Single2,; 

public  class MainTest {
     / ** 
     * Example lazy single mode: asynchronous 
     * advantages: single embodiment, when the initialization call 
     * Disadvantages: not thread synchronization is not recommended 
     * @param args
      * / 
    public  static  void main (String [] args) { 
        CarBean CAR1 = CarBean.getInstance (); 
        CarBean CAR2 = CarBean.getInstance (); 
        car1.setName ( "Lamborghini" ); 
        car2.setName ( "Maserati" ); 
        System.out.println (CAR1 .getName ()); 
        System.out.println (car2.getName ()); 
    } 
}

  Example lazy single mode (synchronous): 

Package Single 3; 

public  class CarBean { 

    public  static CarBean CarBean = null ; 

    Private String name; 

    public String getName () {
         return name; 
    } 

    public  void the setName (String name) {
         the this .name = name; 
    } 

    // closed configuration preventing method. New objects 
    Private CarBean () { 
        System.out.println ( "go only once CarBean constructor" ); 
    } 

    / ** 
     * 1 
     * synchronized using the synchronization lock mode to implement the entire thread synchronization method of synchronizing synchronizing the overall process but in a high in the case of concurrent causes low blocking efficiency 
     * not recommended
     * @Return 
     * / 
    public  static  synchronized CarBean getInstance1 () {
         IF (CarBean == null ) { 
            CarBean = new new CarBean (); 
        } 
        return CarBean; 
    } 

    / ** 
     block * Method of Example 2 using synchronized synchronous change a single embodiment of ; but the method is not completely synchronized, may result in multiple instances; 
     * For example: when you first create multiple threads simultaneously enter if (lazySingleton == null) will produce multiple instances 
     * not recommended 
     * @return 
     * / 
    public  static CarBean getInstance2 () {
         IF (CarBean == null ) {
             the synchronized(. CarBean class ) { 
                CarBean = new new CarBean (); 
            } 
        } 
        return CarBean; 
    } 

    / ** 
     * Method 3: double-checking method checks synchronization code block if w is null once again addresses the issue recommended 
     * / 
    public  static CarBean getInstance3 () {
         IF (CarBean == null ) {
             the synchronized (CarBean. class ) {
                 IF (CarBean == null ) { 
                    CarBean = new new CarBean (); 
                } 
            }
        }
        return carBean;
    }

}

Specific advantages and disadvantages which I have written in the code comments.

Each model will later write again.

Guess you like

Origin www.cnblogs.com/cxiaocai/p/11572564.html