Java design mode (II)-singleton Mode

Source: Internet
Author: User

In Singleton mode, we recommend that you only have one instance for an object. When there is only one object to coordinate the operations of the entire system, driver objects of thread pools, caches, log objects, dialog boxes, printers, and graphics cards are often designed as singleton. In short, the singleton mode is selected to avoid inconsistency, avoid multiple government departments.

The following is a class diagram of the singleton mode: including the private and static modified instance, a private constructor, and a static getInstance function.

There are three main Singleton modes: lazy Singleton, hungry Singleton, and registered Singleton.

1. Hunger type singleton: an instance is created during class initialization.

Class Singleton1 {public Singleton1 () {}; // self-instantiate static private final Singleton1 instance = new Singleton1 (); // obtain the unique public static Singleton1 getInstance () {return instance ;}}

2. Lazy singleton: create a unique instance when needed

// Class Singleton3 {public Singleton3 () {}; static private Singleton3 instance = null; // obtain the unique public static synchronized Singleton3 getInstance () {if (null = instance) {return new Singleton3 () ;}return instance ;}}

3. Registration type singleton: A unique instance is obtained by name matching similar to Spring.

Class Singleton4 {public Singleton4 () {}; static private Map
 
  
Map = new HashMap
  
   
(); Static {Singleton4 single = new Singleton4 (); map. put (single. getClass (). getName (), single);} // obtain a unique instance named public static synchronized Singleton4 getInstance (String name) {if (null = name) {name = Singleton4.class. getName (); System. out. println ("name = null" + "---> name =" + name);} if (null = map. get (name) {try {map. put (name, (Singleton4) Class. forName (Singleton4.class. getName ()). newInstance ();} catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace () ;}} return map. get (name);} public String about () {return "Hello, I am RegSingleton. ";}}
  
 
Main function:

Public class MySingleton {public static void main (String [] args) {Singleton4 single4 = Singleton4.getInstance (null); Singleton4 single5 = Singleton4.getInstance (null ); singleton4 single6 = Singleton4.getInstance ("aaa"); Singleton4 single7 = Singleton4.getInstance ("bbb"); if (single4 = single5) {System. out. println ("single4 and single5 are the same instance");} else {System. out. println ("single4 and single5 are not the same instance");} if (single6 = single7) {System. out. println ("single46 and single7 are the same instance");} else {System. out. println ("single6 and single7 are not the same instance ");}}
Output result:
Name = null ---> name = com. Singleton4name = null ---> name = com. Singleton4single4 and single5 are the same instance. single6 and single7 are not the same instance.


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.