Home  >  Article  >  Java  >  A brief discussion on several common design patterns in Java: singleton pattern

A brief discussion on several common design patterns in Java: singleton pattern

无忌哥哥
无忌哥哥Original
2018-07-23 11:25:101735browse

1. Singleton pattern

It is one of the more commonly used design patterns in Java. Its core structure only contains a special class called a singleton. The singleton pattern can ensure that there is only one instance of the class that applies this pattern in the system.

2. Pattern requirements

1. Your own construction method must be privatized

2. Create your own unique instance inside the class

3 , provide a public static method for other objects to obtain the instance

3. Several implementation methods

hunger style

public class Simple {
private static Simple simple = new Simple();
private Simple() {
}
public static Simple getInstance() {
return simple;
}
}

This implementation method is used when the class is loaded Instantiation is done without lazy loading and is thread safe.

Lazy Man Style

public class Slacker {
private static volatile Slacker slacker = null;
private Slacker() {
}
/**
* 最简单的懒汉式实现
* @return
*/
public static Slacker getInstance() {
if(slacker == null) {
slacker = new Slacker();
}
return slacker;
}
}

This implementation is the most basic implementation. The biggest problem is that it does not support multi-threading. The concurrent access method will obtain more than one instance, so strictly speaking it is not a singleton. model.

public class Slacker {
//volatile关键字的作用
//1、保证内存可见性:保证每个线程访问volatile修饰的共享变量时获取到的都是最新的。
//2、防止指令重排序--通过内存屏障实现
private static volatile Slacker slacker = null;
private Slacker() {
}
/**
* 加synchronized关键字解决线程同步问题
* @return
*/
public static synchronized Slacker getInstanceSync() {
if(slacker == null) {
slacker = new Slacker();
}
return slacker;
}
}

This method of adding synchronized locks to methods can solve the problem of concurrent access by threads and implement singletons. However, adding locks will affect efficiency, so it is often not used this way.

Double check lock

public class Slacker {
//volatile关键字的作用
//1、保证内存可见性:保证每个线程访问volatile修饰的共享变量时获取到的都是最新的。
//2、防止指令重排序--通过内存屏障实现
private static volatile Slacker slacker = null;
private Slacker() {
}
/**
* 双重检查锁解决线程同步问题
* @return
*/
public static Slacker getInstanceDoubleLock() {
if(slacker == null) {
synchronized (Slacker.class) {
if(slacker == null) {
slacker = new Slacker();
}
}
}
return slacker;
}
}

This method uses a double lock mechanism, which can adapt to multi-threaded concurrent access and maintain high performance.

Static inner class implementation

/**
* 静态内部类方式实现单例模式
*
* 当StaticInnerClass第一次被加载时,并不需要去加载StaticInnerClassHoler,只有当getInstance()方法第一次被调用时,
* 才会去初始化staticInnerClass,第一次调用getInstance()方法会导致虚拟机加载StaticInnerClassHoler类,这种方法不仅能
* 确保线程安全,也能保证单例的唯一性,同时也延迟了单例的实例化。
*
* @author chenf
*
*/
public class StaticInnerClass {
private StaticInnerClass() {
}
// 静态内部类
private static class StaticInnerClassHoler {
// 在静态内部类中定义外部类的实例
private static StaticInnerClass staticInnerClass = new StaticInnerClass();
}
/**
* 获取时调用静态内部类的类属性获取外部类的实例
*
* @return
*/
public static StaticInnerClass getInstance() {
return StaticInnerClassHoler.staticInnerClass;
}
}

This method can not only achieve multi-threaded safe access, but also use the internal class loading mechanism to achieve lazy loading.

The above is the detailed content of A brief discussion on several common design patterns in Java: singleton pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn