1. Design Mode - singleton

Comparative eight kinds singleton

1. What is a singleton?

It is to make a single-mode embodiment only one instance of the object class in the system memory, typically provided by a static method.

2. Why use single-case model?

For objects frequently create and destroy objects, as well as create heavyweight objects, time-consuming and more, will increase the memory overhead, can improve system performance by Singleton, saving memory resources.

3. eight kinds of design patterns contrast

Method1: hungry man type (constant static method)

public class Singleton{
	private Singleton() {}
	private static final Singleton s = new Singleton();
	public static Singleton getInstance() {
		return s;
	}
}

Comments: This mode provides constant static way of example, the method is simple, to ensure the security thread, but did not do Lazy loading (lazy loading), if there is no method to get the instance of the call, but also loaded the class (the class is loaded there are many cases), create an object and causing waste of resources, if we can use this to ensure that certain instances, this method is recommended, JDK instance of the Runtime class is to get through this method.

Method2: starving formula (static code block)

public class Singleton{
	private Singleton() {}
	private static Singleton s;
	static{
	 	s = new Singleton();
	}
	public static Singleton getInstance() {
		return s;
	}
}

Comments: This method is obtained by the method of Example static block of code, static code block is executed when the class is loaded, the same advantages and disadvantages Method1.

Method3: lazy-1

public class Singleton{
	private Singleton() {}
	private static Singleton s;
	public static Singleton getInstance() {
		if (s == null) {
			s = new Singleton();
		}
		return s;
	}
}

Comment: The way to do lazy loading, single-thread with no problems; but in the case of multiple threads, multiple instances may be created, can not guarantee the security thread, is not available.

Method4: lazy-2

public class Singleton{
	private Singleton() {}
	private static Singleton s;
	public static synchronized Singleton getInstance() {
		if (s == null) {
			s = new Singleton();
		}
		return s;
	}
}

Comment: The way to get through the synchronous method instance, thread synchronization and ensure the lazy loading, but each instance must obtain synchronization method invocation, resulting in low efficiency, but efficiency is not recommended for use.

Method5: lazy-3-

public class Singleton{
	private Singleton() {}
	private static Singleton s;
	public static Singleton getInstance() {
		if (s == null) {
			synchronized (Singleton.class) {
				s = new Singleton();
			}
		}
		return s;
	}
}

Comment: In this way the synchronization code blocks obtained by example, to ensure that the lazy loading, but can not guarantee the synchronization of threads, multi-threaded unavailable.

Method6: double check

public class Singleton{
	private Singleton() {}
	private static volatile Singleton s = null;
	public static Singleton getInstance() {
		if (s == null) {
			synchronized (Singleton.class) {
				if (s == null) {
					s = new Singleton();
				}
			}
		}
		return s;
	}
}

Comment: The way an instance variable with volatile modification, when changes immediately synchronized to the main memory, to ensure thread synchronization, lazy loading, but also solve the problem of efficiency is recommended.

Method7: static inner classes

public class Singleton{
	private Singleton() {}
	static class Single {
		private static final  Singleton s = new Singleton();
	}
	public static Singleton getInstance() {
		return Single.s;
	}
}

Comment: This way with the advantage of a static inner classes, Singleton is not loaded when loading Single, ensuring the lazy loading; Single class was loaded at the time of use, and loaded only once, to ensure the efficiency and thread synchronization issues, recommended.

Method8: Enumeration

enum Singleton{
	INSTANCE;
}

Comment: The way to get through Singleton.INSTANCE examples, the only instance, thread-safe, it is recommended to use.

to sum up:

1. The implementation level, more than eight methods are feasible in a single thread;
2. Multi-threaded level, Method3 and Method5 unavailable;
3. efficiency level, Method3 synchronization method is not recommended;
4. lazy loading level two type Method1,2 hungry man is not recommended;
5. consider the various factors Method6,7,8 recommended;
Published 28 original articles · won praise 1 · views 530

Guess you like

Origin blog.csdn.net/qq_40575302/article/details/104411094