23 Design Modes (1): single-case mode

Source: Internet
Author: User

Definition: Ensure that a class has only one instance, and that it instantiates itself and provides this instance to the system as a whole. Type: Create class pattern Class Diagram: Class diagram Knowledge points: 1, the class diagram is divided into three parts, followed by the class name, properties, methods. 2, to < <开头和以> > Ends with annotation information. 3, modifier + represents public,-on behalf of private, #代表protected, nothing is visible on behalf of the package. 4, the underlined property or method representation is static. 5. Friends who are unfamiliar with the objects in the class diagram can refer to the article: the relationship of the classes in the design pattern. Singleton mode should be the simplest of the 23 design patterns. It has the following elements: 1, the private method of construction. 2, point to the private static reference of your instance. 3, a static public method that takes its own instance as the return value. The singleton pattern is divided into two types according to the timing of the instantiated objects: one is the A Hungry man type single case, the other one is the lazy type single case. A hungry man singleton when a singleton class is loaded, it instantiates an object to its own reference, and the lazy type instantiates the object when the instance method is invoked. The code is as follows: A hungry man singleton public class Singleton {private static Singleton Singleton = new Singleton (), private Singleton () {} public stat IC Singleton getinstance () {return Singleton;}} Lazy single-Case public class Singleton {private static Singleton Singleton; private Singleton () {} public static synchronized singlet On getinstance () {if (singleton==null) {singleton = new Singleton ();} return singleton;}} Advantages of the Singleton mode: 1, there is only one object in memory, saving memory space. 2, avoid frequent creation of destroyed objects, can improve performance. 3, avoid multiple uses of shared resources. 4, can be accessed globally. Application scenario: Because of the advantages of single-case mode, it is a more design pattern used in programming. I've summed up what I know. Suitable for scenarios that use Singleton mode: 1, objects that require frequent instantiation and destruction. 2, objects that are too time consuming to create objects or consume too much resources, but are often used. 3, a stateful tool class object. 4. The object that accesses the database or file frequently. 5, and all the other scenes I didn't use to ask for only one object. Single-Case mode Note: 1, you can only use the method provided by the Singleton class to get a singleton object, do not use reflection, otherwise you will instantiate a new object. 2, do not do a dangerous operation to break a static reference in a singleton class object to a class. 3, multi-threaded use a single instance when using shared resources, noteThreading security issues. Some arguments about the singleton pattern in Java: The object of a singleton pattern is not collected by the JVM garbage collector for long periods of time: If a singleton object is not in memory for long, it will be considered a garbage by the JVM and will be cleaned up when garbage collection is performed. In view of this, the author is skeptical, the author's opinion is: in the hotspot virtual Machine 1.6 version, the JVM garbage collector will not recycle the singleton object unless it is manually disconnected from the singleton in the case of a static reference to the Singleton object. For this controversy, I wrote a separate article to discuss, if you have a different point of view or have experienced this experience please enter the article singleton mode discussion: Singleton mode and garbage collection participate in the discussion. Will there be more than one singleton in a JVM? In the case of distributed systems, multiple ClassLoader, and serialization, there is no doubt that multiple singleton cases are generated. Is it possible to create a singleton in the same JVM? Only the same singleton can be obtained using the getinstance () method provided by the Singleton, except that a new singleton will be obtained if the reflection method is used. The code is as follows Class C = Class.forName (Singleton.class.getName ()); Constructor ct = c.getdeclaredconstructor (); Ct.setaccessible (TRUE); Singleton Singleton = (Singleton) ct.newinstance () so that each run produces a new singleton object. So when using singleton mode, be careful not to use reflection to produce new singleton objects. Lazy one-thread-safe? Mainly on the Internet some say, lazy-type singleton mode is thread unsafe, even in the method of instantiating objects add synchronized keyword, it is still dangerous, but the author passed the code test, found that add synchronized keyword modification, although the performance has some impact, However, it is thread-safe and does not result in instances where multiple objects are instantiated. Are there only two types of a hungry man and lazy type in singleton mode? A hungry man single-and lazy-type singleton is just two main and common single-case mode, theoretically, any design pattern that can implement only one instance of a class can be called a singleton pattern. Can a singleton class be inherited? A hungry man single and lazy single cases because the construction method is private, they are not inheritable, but many other singleton patterns can be inherited, such as the registered singleton. A hungry man type single case good or lazy type single case good? In Java, the A hungry man is superior to the lazy one-case. In C + +, lazy-type singleton is generally used.

23 Design Modes (1): Singleton mode

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.