Single-Case mode

Source: Internet
Author: User

In general, design patterns fall into three broad categories:

Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.

There are 11 types of behavioral Patterns: Strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

The singleton pattern ensures that there is only one instance of a class, and that it provides this instance to the system by itself, and the singleton pattern is divided into lazy and a hungry man types:

A hungry man is thread-safe, but relatively time-saving:

1 classsingleton1{2     Private StaticSingleton1 instance =NewSingleton1 ();3     4     PrivateSingleton1 () {}5     6      Public StaticSingleton1 getinstance () {7         returninstance;8     }9}

Lazy if not locking is not thread-safe, but relatively wasteful running time:

1 classsingleton2{2     Private StaticSingleton2 instance =NULL;3     4     PrivateSingleton2 () {}5     6      Public Static Singleton2 getinstance () {//Add synchronized keyword to the method is thread-safe7         if(NULL==instance)8Instance =NewSingleton2 ();9         returninstance;Ten     } One}

In addition, a hungry man also has a way of using an inner class implementation:

1 classsingleton3{2     Static classinner{3         StaticSingleton3 instance =NewSingleton3 ();4     }5     6     PrivateSingleton3 () {}7     8      Public StaticSingleton3 getinstance () {9         returninner.instance;Ten     } One}

In the development, the use of a hungry man-style more.

Single-Case 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.