About singleton pattern and its Java implementation

Singleton is 23 design patterns in a relatively simple, this talk about the Singleton pattern.

1. What is a design pattern?

  For the people do not come into contact with design patterns, the design pattern hear the words you think this thing is very enigmatic, suddenly had a fear of this thing feeling, in fact, design patterns are those Gangster experience in project comprehend it and summed up routines, these routines can be used to address specific issues in project development, design patterns are not so terrible (in fact, was Huangde number ...). A total of 23 kinds of design patterns, classified according to use, can be divided into three categories:

  • Create a schema : singleton, factory pattern, abstract factory pattern, the builder pattern, prototype model
  • Structural model: adapter mode, bridge mode, decorative patterns, combined mode, the appearance model, Flyweight, proxy mode
  • Behavioral patterns: template access mode, command mode, iterative mode, observer mode, intermediary model, the memo mode, the interpreter pattern, state pattern, strategy pattern, chain of responsibility pattern, the visitor mode

Example 2. The effect of single-mode

Ensure that only one instance of a class, and provides access to the instance's global access point

Example 3. Single application scenario mode

  • The Windows Task Manager (Task Manager) is a typical singleton
  • the windows Recycle Bin (trash) is a typical embodiment of a single application. During the entire operation of the system, it maintains the Recycle Bin has been only one instance.
  • Project, the class reads the configuration file, usually only one object. Every time there is no need to use the profile data each time a new object is to read.
  • Website counter, are generally single-case model to achieve, otherwise difficult to synchronize.
  • Application Log application, generally realized what a singleton pattern, which is generally due to the shared log file remains open, because only one instance to operate, otherwise good additional content.
  • Database connection pool designs are generally single-mode embodiment, because the database is a database connection resource.
  • The operating system's file system, a specific example embodiment is a large single-mode implementation, an operating system can have a file system.
  • Typical applications are the singleton Application (Servlet programming will be involved)
  • In Spring, each default Bean is singleton is the advantage that a container can be managed Spring
  • In the servlet program, each embodiment is also a single Servlet
  • In the spring MVC frame / struts1 frame, the object is also a single embodiment of the controller

Example 4. Single-mode implementation ( the Java )

4.1 starving formula

the Singleton Package;
/ **
 * Example starving single mode
 * @author CJJ
 * /
public class Singleton01 {
    // create an instance of a static properties
    Private static Singleton01 new new instance Singleton01 = ();
    // constructor privatization
    private Singleton01 ( ) {}
    // return the instance
    public static Singleton01 getInctance () {
        return instance;
    }
}

Why this implementation is called starving type it? Because of this implementation is to create an instance of a static properties, static properties already created instance of the class when loaded, this time has not yet begun to use examples too, so vividly described as a hungry man to achieve the formula (manual Kobold programmers are not all boring animals ...), so that implementation can cause memory loss.

4.2 lazy style

the Singleton Package;
/ **
 * Example lazy single mode
 * @author CJJ
 *
 * /
public class Singleton02 {
        // constructor privatization
    Private Singleton02 () {
    }
    Private static Singleton02 instance;
    public static the synchronized Singleton02 the getInstance () {
        IF ( ! instance = null) {
            instance Singleton02 new new = ();
        }
        return instance;
    }
}

In sharp contrast hungry man is lazy type of style, this implementation is to create an instance when called. Buy tickets parable to illustrate two implementations, a hungry man is one of those type travel the next day, the previous day, it had booked a ticket, but lazy man does this guy in front of the car a minute before departure bought tickets. Compared with the hungry man style, this implementation seems more reasonable, however, this implementation addresses the shortcomings of a hungry man style, but also brought new problems - will create slower speed. So-called "not the best algorithm, only suitable algorithm, space complexity and time complexity be forced to!"

4.3 static inner class creates an instance

package Singleton;

/ **
 Examples static internal * Create
 * @author CJJ
 *
 * /
public class Singleton04 {
   
    // static class to create an instance of an internal
    static class the SingleInstance {
        public static Final Singleton04 new new instance Singleton04 = ();
    }
    // configured privatization is
    Private Singleton04 () {}
    // return the instance
    public static Singleton04 the getInstance () {
        return SingleInstance.instance;
    }
   
}

This implementation combines the advantages of starving and lazy Formula Formula of the two methods, i.e., when loading a class (static internal) calls and create an instance.

4.4 Enumerated create a singleton

package Singleton;

public enum Singleton05 {
    INSTAANCE;
    public void Operation() {
       
    }
}

This created a simple and clear way, design is only one instance INSTAANCE in the enumeration class, and then add the operation of a single case of it, add the enumeration nature also belongs to a class, each enumeration type are inherited from java .lang.Enum <> class, an enumeration type may also be members of the method. This method avoids the use of reflection to invoke the private constructor to create an instance.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160196.htm