单例模式
1.饿汉式(静态常量)/(静态代码块)
代码
package singleton.type1;
public class SingleTest01 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
//饿汉式(静态变量)
class Singleton{
//1.构造器私有化,外部不能new
private Singleton(){}
//2.本类内部创建对象实例
private final static Singleton instance = new Singleton();
//3.对外提供公有的静态方法返回实例对象
public static Singleton getInstance() {
return instance;
}
}
//----------------------------------------------------------------------------
package singleton.type2;
public class SingleTest02 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
//饿汉式(静态代码块儿)
class Singleton{
//1.构造器私有化,外部不能new
private Singleton(){}
//2.本类内部创建对象实例
private static Singleton instance ;
static{
instance=new Singleton();
}
//3.对外提供公有的静态方法返回实例对象
public static Singleton getInstance() {
return instance;
}
}
优缺点:
简单,但没有实现懒加载机制,会造成内存浪费
2.懒汉式(线程不安全)
代码
package singleton.type3;
public class SingleTest03 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
//懒汉式(线程不安全)
class Singleton{
private static Singleton instance;
//1.构造器私有化,外部不能new
private Singleton(){}
//提供一个静态的公有方法,当使用到该方法时,才去创建instance
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3.懒汉式(线程安全,同步方法)
public class SingleTest04 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
//懒汉式(线程安全,同步方法)
class Singleton{
private static Singleton instance;
//1.构造器私有化,外部不能new
private Singleton(){}
//提供一个静态的公有方法,当使用到该方法时,才去创建instance
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
效率太低
4.懒汉式(线程安全,双重检查,推荐使用)
public class SingleTest06 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
//懒汉式(线程安全,双重检查)
class Singleton{
private static volatile Singleton instance;
//1.构造器私有化,外部不能new
private Singleton(){}
//提供一个静态的公有方法,加入双重检查代码,解决线程安全问题,同时解决懒加载的问题
public static synchronized Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class){
if (null == instance ){
instance = new Singleton();
}
}
}
return instance;
}
}
推荐使用,即解决了线程安全问题,又实现了懒加载。
5.静态内部类(推荐使用)
public class SingleTest07 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
//静态内部类
class Singleton{
//1.构造器私有化,外部不能new
private Singleton(){}
//写一个静态内部类,该类中有一个静态属性Singleton
private static class SinglentonInstance{
//jvm类加载时时线程安全的, 静态类只加载一次时单例的。
private static final Singleton INSTANCE=new Singleton();
}
// 提供一个静态的共有的方法返回实例
public static Singleton getInstance() {
return SinglentonInstance.INSTANCE;
}
}
6.枚举(推荐使用)
public class SingletonTest08 {
public static void main(String[] args) {
Singleton instance = Singleton.INSTANCE;
Singleton instance1 = Singleton.INSTANCE;
System.out.println(instance==instance1);
}
}
enum Singleton{
INSTANCE;
public void sayOK(){
System.out.println("ok~");
}
}
标签:Singleton,getInstance,模式,class,instance,static,单例,public
From: https://www.cnblogs.com/yufou/p/16731243.html