A single example model for practical cases of design patterns

Source: Internet
Author: User

The scenario for a singleton pattern is that there are some things in the system that provide basic, fixed services that do not require flexible operation and change, and are primarily for get operations.

Therefore, we do not need to create the object of the class to operate, which can improve the performance and efficiency of the system.

For example, get configuration file information, get database connection pool, etc.

Let's take a look at an example of getting configuration file information:

ImportCom.jfinal.kit.Prop;ImportCom.jfinal.kit.PropKit;/*** Configure helper Classes*/ Public classConfigutil {Private StaticProp Prop =NULL; Static{        if(Prop = =NULL) Prop= Propkit.use ("Cpsis.properties"); }    /*** Get real-time Database profile Property Object *@return     */     Public StaticProp Getprop () {returnprop; }    /*** Get real-time database implementation interface class *@return     */     Public StaticString Getrtdbclassname () {returnProp.get ("Rtdb.classname"); }
/**     * There are a number of other static get functions that we can use to get configuration information     * @return * *     

}

The singleton pattern needs to be used with the static property and the static method. Because only the static properties and methods of the class are accessible without creating a variable, we can use this static property to store objects that we only need an instance of.

There are two main advantages of the singleton mode:

1) Reduce the overhead associated with creating Java instances

2) It is easy for the system to track the life cycle, instance status, etc. of a single Java instance.

For the understanding of static variables and static methods, please refer to Baidu.

The preceding code uses a static block to initialize the singleton object, or it can be done statically:

 Public class Singleton    {        privatestatic  Singleton instance;         Private Singleton ()        {        }        publicstatic  Singleton getinstance ()        {              Returnnew  Singleton ());        }    }

Supplement, Hibernate application

Hibernate Sessionfactory is a heavyweight class, creating an object instance of that class consumes a lot of system resources, and if you create an instance of that class every time you need it, it obviously reduces the execution efficiency of the program, so you often put an instantiation of that class in a static{} , you only need to execute the first call and improve the execution efficiency of the program as follows:

Static {     try  {   configuration.configure (configfile);    = configuration.buildsessionfactory ();   Catch (Exception e) {   System.err.println ("%%%% Error Creating sessionfactory%%%%");   E.printstacktrace ();  }    }

A single example model for practical cases of design patterns

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.