Difference between singleton and multiple instance

Singleton and multiple instances need to understand two questions:
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 Example;
1. What are singletons and multiple instances:
The so-called singleton means that all requests are handled by one object. For example, the objects of our commonly used service and dao layers are usually singletons, while multiple instances refer to each The request is handled with a new object, such as action; 

1. Description of singleton mode and multi-instance mode:

1. The singleton pattern and the multi-instance pattern belong to the object pattern.

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 examples

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;

/**

 * 单例模式测试

 * @author solid

 *

 */

public class TestSingleton {

 

    private static Singleton singleton;

   

    public static void main(String[] args) {

       singleton = Singleton.getInstance();

       singleton.getMessage("title");

    }

}

 

2.         多例模式举例:

package com.solid.pattern;

/**

 * 多例模式

 * @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;

       }

    }

   

    /**

     * 获取1—6之间的随机数

     */

    public void getValue() {

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

       System.out.println(value);

    }

}

 

package com.solid.pattern;

/**

 * 多例模式测试

 * @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. 如何产生单例、多例:
    在通用的SSH中,单例在spring中是默认的,如果要产生多例,则在配置文件的bean中添加scope="prototype"; 

我就告诉你昨天我得问题你估计就明白了

我没用scope="prototype"就出现上面得验证问题了 连续点提交就这样
我添加后 不论怎么点都只会出现一个验证提示

3. 为什么用单例、多例:
    之所以用单例,是因为没必要每个请求都新建一个对象,这样子既浪费CPU又浪费内存;
   之所以用多例,是为了防止并发问题;即一个请求改变了对象的状态,此时对象又处理另一个请求,而之前请求对对象状态的改变导致了对象对另一个请求做了错误的处理;
    用单例和多例的标准只有一个:
    当对象含有可改变的状态时(更精确的说就是在实际应用中该状态会改变),则多例,否则单例;
4. 何时用单例?何时用多例?
    对于struts2来说,action必须用多例,因为action本身含有请求参数的值,即可改变的状态;
  而对于STRUTS1来说,action则可用单例,因为请求参数的值是放在actionForm中,而非action中的;
   另外要说一下,并不是说service或dao一定是单例,标准同第3点所讲的,就曾见过有的service中也包含了可改变的状态,同时执行方法也依赖该状态,但一样用的单例,这样就会出现隐藏的BUG,而并发的BUG通常很难重现和查找;


Guess you like

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