Java single-instance mode implementation

Source: Internet
Author: User

A class that can only generate one instance is a class that implements the singleton (singleton) pattern. Here's how to implement a singleton pattern in C #

Mode one is only used in single-threaded environments

Set the constructor as a private function to prevent others from creating instances//Define a static instance, create the instance when needed//in Singleton static property instance, only create an instance when instance is null to avoid duplicate creation//  Define the constructor as private function public final class Singleton1 {private Singleton1 () {}private static Singleton1 _instance = Null;public static Singleton1 getinstance () {if (_instance = = null) _instance = new Singleton1 (); return _instance;} public static void Main (string[] args) {Singleton1 S1 = singleton1.getinstance (); Singleton1 s2 = singleton1.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}}


Mode two plus synchronous lock

import java.util.concurrent.locks.lock;import  java.util.concurrent.locks.reentrantlock;public final class singleton2 {private  Singleton2 () {}static lock lock = new reentrantlock ();p rivate static  Singleton2 _instance = null;public static singleton2 getinstance () {lock.lock (); Try {if (_instance == null) _instance = new singleton2 ();}  finally {lock.unlock ();} Return _instance;} Public static void main (String[] args)  {Singleton2 s1 =  Singleton2.getinstance (); Singleton2 s2 = singleton2.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}} 


Feasible solution plus synchronous lock two times before and after the instance has been determined whether it exists

import java.util.concurrent.locks.lock;import  java.util.concurrent.locks.reentrantlock;public final class singleton3 {private  Singleton3 () {}static lock lock = new reentrantlock ();p rivate static  Singleton3 _instance = null;public static singleton3 getinstance () {if (_instance  == null) {lock.lock (); Try{if (_instance == null) _instance = new singleton3 () ;} Finally {lock.unlock ();}} Return _instance;} Public static void main (String[] args)  {Singleton3 s1 =  Singleton3.getinstance (); Singleton3 s2 = singleton3.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}} 


The recommended solution is to use a static constructor

Public final class Singleton4 {private Singleton4 () {}private static Singleton4 _instance = new Singleton4 ();p ublic static Singleton4 getinstance () {return _instance;} public static void Main (string[] args) {Singleton4 S1 = singleton4.getinstance (); Singleton4 s2 = singleton4.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}}


Recommended solution Two implementations create instances on demand

public class Singleton5 {private Singleton5 () {}public static Singleton5 getinstance () {return nested.instance;} private static Class Nested{private Nested () {}public static final Singleton5 instance = new Singleton5 ();} public static void Main (string[] args) {Singleton5 S1 = singleton5.getinstance (); Singleton5 s2 = singleton5.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}}


    1. Extended

    定义一个表示总统的类型President可以从该类型继承出FrenchPresident和AmericanPresident等类型。这些派生类型都只能产生一个实例        

Public class president {private string _name;public president () {}public  String getname () {return _name;} Public void setname (string name) {_name = name;} Public static void main (String[] args)  {}}public class frenchpresident  extends president{private frenchpresident () {}public static frenchpresident  GetInstance () {return nested.instance;} Private static class nested{private nested () {}public static final  Frenchpresident instance = new frenchpresident ();} Public static void main (String[] args)  {FrenchPresident s1 =  Frenchpresident.getinstance (); Frenchpresident s2 = frenchpresident.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}} Public class americanpresident {private americanpresident ()  {}pUblic static americanpresident getinstance ()  {return nested.instance;} Private static class nested {private nested ()  {}public static final  americanpresident instance = new americanpresident ();} Public static void main (String[] args)  {AmericanPresident s1 =  Americanpresident.getinstance (); Americanpresident s2 = americanpresident.getinstance (); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ());}}



This article is from the "Xu Big Tree" blog, please be sure to keep this source http://abelxu.blog.51cto.com/9909959/1965630

Java single-instance mode implementation

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.