Difference between singleton and multiple instances in java

https://www.cnblogs.com/zhangliang88/p/5388472.html

Singleton and multiple instances need to understand two problems:

1. What is a single instance and multiple instances;
2. How to generate a single instance and multiple instances;
3. Why use a single instance and multiple instances
4. When to use a single instance and when to use multiple instances;
1. What is a single instance and multiple instances :
The so-called singleton means that all requests are handled by an object. For example, the objects of our commonly used service and dao layers are usually singletons, while multiple instances mean that each request is handled by a new object, such as action ; 


First, the singleton mode and the multi-case mode description:


1. The single-case mode and the multi-case mode belong to the object mode.


2. There is only one copy of the object in the singleton mode in the entire system, and there can be multiple instances in the multi-case mode.


3. None of them provide external constructors, that is, the constructors are all private.


2. Application example


1. Example of singleton pattern:

package com.solid.pattern;


import java.util.Locale;


import java.util.ResourceBundle;


/**


 * Singleton mode


 */


public class Singleton {


    private static Singleton singleton = new Singleton();


    private Singleton() {}


    public static Singleton getInstance() {


       return singleton;


    }


   


    /**


     * Get the value of the configuration file


     * @param key


     */


    public void getMessage(String key) {


       Locale locale = new Locale(key);


       ResourceBundle res = ResourceBundle.getBundle("res_zh_CN");


       String message = res.getString(key);


       System.out.println(message);


    }


}


 


package com.solid.pattern;


/**


 * Singleton mode test


 * @author solid


 *


 */


public class TestSingleton {


 


    private static Singleton singleton;


   


    public static void main(String[] args) {


       singleton = Singleton.getInstance();


       singleton.getMessage("title");


    }


}



 


2. Example of multi-instance mode:


package com.solid.pattern;


/**


 * Multiple instance mode


 * @author solid


 *


 */


public class Multiton {


 


    private static Multiton multi1 = new Multiton();


    private static Multiton multi2 = new Multiton();


   


    private Multiton() {}


   


    public static Multiton getInstance(int key) {


       if(key == 1) {


           return multi1;


       } else {


           return multi2;


       }


    }


   


    /**


     * Get a random number between 1 and 6


     */


    public void getValue() {


       int value = (int)(Math.random()*6+1);


       System.out.println(value);


    }


}


 


package com.solid.pattern;


/**


 * Multi-case mode test


 * @author solid


 *


 */


public class TestMultiton {


    private static Multiton multi1;


    private static Multiton multi2;


    public static void main(String[] args) {


       multi1 = Multiton.getInstance(1);


       multi2 = Multiton.getInstance(2);


      


       multi1.getValue();


       multi2.getValue();


    }


}




2. How to generate singleton and multiple instances:
    In general SSH, singleton is the default in spring. If you want to generate multiple instances, add scope="prototype" to the bean of the configuration file; 


I will tell you yesterday I have a problem, you can probably understand it.


I did not use scope="prototype", and the above verification problem occurred. Continuously submit it.
After I add it, no matter how I click, only one verification prompt will appear.
3. Why use singleton and multiple examples :
    The reason for using a single instance is that it is not necessary to create a new object for each request, which wastes both CPU and memory;
   the reason for using multiple instances is to prevent concurrency problems; that is, a request changes the state of the object, which When the object handles another request, and the previous request's change to the object's state causes the object to mishandle the other request;
    there is only one criterion for singleton and multi-instance:
    when the object contains changeable state (more To be precise, the state will change in practical applications), then there are multiple instances, otherwise singletons;
4. When to use singletons? When to use multiple instances?
    For struts2, action must use multiple instances, because the action itself contains the value of the request parameter, which can be changed;
  for STRUTS1, the action can use a single instance, because the value of the request parameter is placed in the actionForm, and Not in action; in
   addition, it does not mean that service or dao must be a singleton. The standard is the same as that mentioned in point 3. I have seen that some services also contain changeable states, and the execution method is also Relying on this state, but using the same singleton, there will be hidden bugs, and concurrent bugs are usually difficult to reproduce and find;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326007666&siteId=291194637