Java Single Example Class Demo__java

Source: Internet
Author: User
Tags serialization static class volatile
It is not recommended to use Singleton1 and Singleton2,singleton3 basic to meet the requirements, SINGLETON5 support delay loading, but the implementation of a bit of trouble, effective Java authors recommend the enumeration to implement a single case, automatic support serialization mechanism , preventing deserialization from recreating new objects, and absolutely preventing multiple instantiations
implementing a single example by enumerating
Package Com.basic.designpatterns.create.singleton;

/** *
 Single Case
 * *
 @author chichuduxing
 * @date February 4, 2017 pm 4:40:22

/class Resource {
	//specific business. c10/>public void DoSomething () {
		System.out.println ("working");
	}

/**
 * Supports thread security by enumerating the class implementations (the constructs must be private in the enumeration class), supporting serialization */public
enum Enumsingleton {

	INSTANCE;

	Private Resource instance = null;

	Private Enumsingleton () {
		instance = new Resource ();
	}

	Public Resource getinstance () {return
		instance;
	}
}


Single case class that implements a single example by enumerating, but does not expose details

Package Com.basic.designpatterns.create.singleton;

/**
 * @author chichuduxing
 * @date April 3, 2017 pm 7:54:42/public

class Enumfactory {

	/**
	 * A method of obtaining a single case from abroad
	 *
	 * @return Single
	 object
	/public static Singresource getinstance () {
		return EnumSingleton.INSTANCE.getInstance ();
	}

	/**
	 * Supports thread security by enumerating class implementations (constructs must be private in the enumeration class), supporting serialization/
	private enum Enumsingleton {

		INSTANCE;

		Private Singresource instance = null;

		Private Enumsingleton () {
			instance = new Singresource ();
		}

		Public Singresource getinstance () {return
			instance;
		}
	}

	/**
	 * Test Method
	 *
	 * @param args
	/public static void main (string[] args) {
		Enumfactory.getinstance (). dosomething ();
	}

/**
 * Specific implementation of the function of the single example class
/class Singresource {public
	Singresource () {

	}

	//specific business.
	public void DoSomething () {
		System.out.println ("working");
	}




several other ways of implementing

Package Com.basic.designpatterns.create.singleton; /** * Single Case Mode * * @author chichuduxing * @date February 4, 2017 11:19:02//** * lazy, thread unsafe * * Class Singleton1 {privat

	e Singleton1 () {} private static Singleton1 instance = NULL;
		public static Singleton1 getinstance () {if (null = = instance) {return new Singleton1 ();
	return instance; 

	}/** * lazy, thread-safe Efficiency low * * Class Singleton2 {private Singleton2 () {} private static Singleton2 instance = NULL;
		public static synchronized Singleton2 getinstance () {if (null = = instance) {return new Singleton2 ();
	return instance;
 }/** * A hungry man, thread-safe when a single classloader is not locked, threads are safe, but the class is loaded with a new object.

	* * Class Singleton3 {private Singleton3 () {} private static Singleton3 instance = new Singleton3 ();
	public static Singleton3 getinstance () {return instance; }/** * Double lock check lazy, thread safe. Need to add volatile keyword </br> * Because instance = new Singleton4 (); not atomic Operation,</br> * If the processor is reordered, you can Can instance in the morning although not NULL, but the instance has notInitialization </br>/* class Singleton4 {private Singleton4 () {}//required add volatile keyword private volatile static Singleton4

	instance = null; public static Singleton4 getinstance () {if (instance = null) {synchronized (Singleton4.class) {if (null = = in
				Stance) {instance = new Singleton4 ();
	}} return instance; }/** * Internal class, the internal class is deferred loading, will only be loaded on the first use, do not load without using.
 Lazy, thread-safe. * * Class Singleton5 {private Singleton5 () {} static class Innersingleton {private static final Singleton5 Instan
	CE = new Singleton5 ();
	public static final Singleton5 getinstance () {return innersingleton.instance;
		} public class Singletonpatterndemo {//Recommended Enumsingleton//second SINGLETON5 public static void Main (string[) args) {
	EnumSingleton.INSTANCE.getInstance (). dosomething ();
 }
}





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.