Follow the example learning design pattern-Singleton mode

Source: Internet
Author: User



Singleton mode belongs to the creation design pattern.

The singleton pattern is designed to guarantee that a class has only one instance and provides a global access point to access it.

The singleton model is based on a single case, one of which is a 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 type:

Package com.home.singleton;/** * A hungry man type *  * * * * @author GAOXU Practice! */public class Singletonb {static singletonb singletonb = new singletonb (); private singletonb () {}; public static Singl Etonb getinstance () {  return singletonb;}}



Lazy Type:

Package com.home.singleton;/** * Lazy type Single case *  * @author Gaoxu Practice! */public class Singletona {static Singletona singleton = Null;private Singletona () {};p ublic static synchronized Singleton A getinstance () {if (singleton = = null) {return new Singletona ();} return singleton;}}

The singleton pattern is the simplest of the design patterns, and it has the following elements:

    • Private method of construction
    • private static reference to your own instance
    • Static public method that takes its own instance as the return value

We have seen how to construct a singleton class, so what is the use of simply writing this single case class in practice? Let's take a look at an example.

We know that spring's IOC has two ways of loading, one is that Beanfactory is a appicationcontext, so how do we use a singleton to load an IOC container?

Import Org.slf4j.logger;import Org.slf4j.loggerfactory;import org.springframework.context.ApplicationContext; Import org.springframework.context.support.classpathxmlapplicationcontext;/** * @author Gaoxu Practice! */public class Springtools {protected static Logger log = Loggerfactory.getlogger (springtools.class);p rivate ApplicationContext context;/** * will only be loaded when called * *  */private static class Singletionholder {private static springtools in stance = new Springtools ();} Private Springtools () {log.info ("Init spring Context"), context = new Classpathxmlapplicationcontext ("Application.xml" );} public static ApplicationContext GetContext () {return SingletionHolder.instance.context;}}

The code above is a singleton class that loads the spring IOC container through Appictioncontext, and we can load it in the main startup class of the application as follows.

ApplicationContext context = Springtools.getcontext ();

This allows us to access the classes in the container through the methods provided by the interface.

Advantages of Singleton mode:

    • There is only one object in memory that saves memory space.
    • Avoid frequent creation of destroyed objects, which can improve performance.
    • Avoid multiple uses of shared resources.
    • can be accessed globally.

We can also actually experience these advantages of the singleton pattern through the examples just now, and the spring IOC container accesses are quite frequent in the application, so the singleton mode loading is suitable.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Follow the example to learn design patterns-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.