Design Patterns Mode

Source: Internet
Author: User

1. Single case design mode (singleton)

Examples of use: When using the same configuration information object for multiple programs, such as using singleton mode when connecting to a database, only one connection is taken out at a time

Step: ① privatize the constructor for this class

② privatization of a static object

③ public A static method that returns the Created object

Lazy Style Templates:

/**@author  */Publicclass  Singleton    {  Private  Singleton () {    }    privatestaticnew  Singleton ();      Public Static Singleton getinstance () {        return  Singleton;    }}

A hungry man-style templates:

/*** A hungry man: lazy initialization of this singleton instance, to consider multi-threaded simultaneous request initialization problem * Delay initialization reason: ① static initialization, there is not enough information to initialize the singleton *② and get resources, such as database connection, especially in a particular session, it package When the object instance is not required by the containing application **/ Public classSingletonofhungry {Private StaticObject Classlock = singletonofhungry.class;
Privatesingletonofhungry () {}Private StaticSingletonofhungry singletonofhungry =NULL; Public Staticsingletonofhungry getinstance () {synchronized(Classlock) {//syncif(Singletonofhungry = =NULL) {singletonofhungry=Newsingletonofhungry (); } returnsingletonofhungry; } }}

A Hungry Man type Demo:

/*** Uninitialized a Hungry man single-case factory*/ Public classFactory {Private Longnum; Private StaticObject Classlock = Factory.class; PrivateFactory () {num= 0; }    Private StaticFactory Factory;  Public StaticFactory getfactory () {synchronized(classlock) {if(factory==NULL) {Factory=NewFactory (); }            returnFactory; }    }     Public voidRecordnum () {synchronized(classlock) {num++; }    }}

Getting an instance can also be written like this:

 Public Static Factory getfactory () {        if(factory==null) {     //cannot be locked             after instantiation Synchronized  (classlock) {                if (factory==null) {    //prevents locking, is instantiated by another thread and does not need to instantiate                     new  Factory ()        ; }}} return factory;    }

Design Patterns 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.