Design Pattern-single-column Pattern

Source: Internet
Author: User

I. Singleton mode definition:

The Singleton mode is a simple mode, which is defined as: ensure that a class has only one instance, and instantiate it on its own and provide the instance like the whole system. The Singleton mode can be roughly divided into the hungry and lazy modes.

The advantages, disadvantages, and use cases of the singleton mode are excerpted from Zen of design patterns.

 

Ii. Advantages of Singleton mode:

1. The Singleton mode only has one instance in the memory, which reduces memory expenses. In particular, an object needs to be created and destroyed frequently, and the performance cannot be optimized during creation or destruction, the advantages of Singleton mode are even more obvious.

2. Because the singleton mode only generates one instance, the system performance overhead is reduced. When an object requires more resources, such as reading configurations and generating other dependent objects, you can directly generate a singleton object when the application starts, and then use the permanent memory resident method to solve the problem. (in java ee, the garbage collection mechanism of JVM should be noted in the singleton mode)

3. The Singleton mode can avoid multiple resource occupation. For example, a file write action can only exist in one instance to avoid simultaneous write operations on the same resource file.

4. In Singleton mode, you can set global access points in the system to optimize and share resource access. For example, you can design a singleton class to map all data tables.

 

Iii. disadvantages of Singleton mode:

1. The Singleton mode generally has no interface, and it is difficult to expand. To expand it, there is basically no second way except to modify the code. Why cannot I Add Interfaces in singleton mode? Because the interface has no significance for the singleton mode, it requires "self-instantiation" and it is impossible to instantiate a single instance, interface, or abstract class. Of course, in special cases, the singleton mode can implement interfaces, be inherited, and so on. It must be determined by the environment during system development.

2. The Singleton mode is unfavorable for testing. In the parallel development environment, if the singleton mode is not completed, the test cannot be performed, and the mock mode cannot be used to virtualize an object without interfaces.

3. The Singleton mode conflicts with the single responsibility principle. A class should only implement one logic, regardless of whether it is a singleton or not. Whether it is a singleton depends on the environment, the Singleton mode integrates the "Singleton" and business logic into a class.

 

Iv. Use Cases of Singleton mode:

In a system, a class must have only one object. If multiple objects exist, an "adverse reaction" will occur. You can use the singleton mode.

1. environment where a unique serial number is required

2. A shared access point or shared data is required for the entire project. For example, a counter on a web page does not need to record every refresh to the database, maintain the counter value in singleton mode and ensure thread safety.

3. Too many resources are required to create an object, such as accessing Io and database resources.

4. If you need to define a large number of static constants and static methods (such as tool classes), you can use the single-sample mode (of course, you can also directly declare it as static)

 

V. Hungry and lazy Singleton mode:

1. Hunger Singleton Mode

1/*** hunger Singleton mode, A singleton 3*4 * @ author lion 5*6 */7 public class Singleton {8 9/Private Static instance variable has been created during class loading, 10 Private Static Singleton = new Singleton (); 11 12 // private constructor has been created during class loading to ensure that 13 private Singleton () is not accessed outside the class () {14} 15 16 // a public static method returns a singleton instance 17 public static Singleton getinstance () {18 return Singleton; 19} 20 21 public static void main (string [] ARGs) {22 // output, test whether the same object is 23 for (INT I = 0; I <3; I ++) {24 system. out. println (Singleton. getinstance (). tostring (); 25} 26} 27}

2. Lazy Singleton Mode

1/** 2 * lazy Singleton mode. Only one instance is created during the first request, no instance will be created in the future, but thread security will occur. 3*4 * @ author lion 5*6 */7 public class singleton1 {8 9 // Private Static instance variable 10 Private Static singleton1 Singleton = NULL; 11 12 // private constructor 13 private singleton1 () {14} 15 16 // public static factory method, return the unique instance of this class (initialized only when no instance is found to have not been initialized) 17 public static singleton1 getinstance () {18/*** <PRE> 20 * thread unsafe Description: 21 * If a thread a executes to singleton = new singleton1 (), but the object has not yet been obtained (initializing); 22 * at this time, thread B also executes to singleton = NULL, so the judgment condition of thread B is true, therefore, Singleton = new singleton1 (); 23 * is executed. In the end, thread A obtains an object, and thread B also obtains an object, therefore, two objects are displayed in the memory 24 * </PRE> 25 */26 if (Singleton = NULL) {27 Singleton = new singleton1 (); 28} 29 return Singleton; 30} 31 32 public static void main (string [] ARGs) {33 system. out. println ("no good test, thread security only appears in the case of a large number of concurrency, low concurrency is the same as the hungry mode"); 34} 35}

 

Well, the singleton mode is just like what I understand now. If there is a new understanding, it will be added.

 

Author: dengtian Road

Reprint please explain the source: http://www.cnblogs.com/travellife/

Design Pattern-single-column Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.