设计模式--单例模式

2019-08-12 17:07:25 浏览数 (1)

  • 1 饿汉式(静态变量)
代码语言:javascript复制
package com.shi.design.singleton;

/**
 * 单例模式:1 饿汉式(静态变量)
 * @author shiye
 *
 */
public class Singleton1 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  2. 本类内部创建对象实例
 *  3. 提供一个共有的静态方法,返回实力对象
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static final Singleton instance = new Singleton();
	
	public static Singleton getInstance() {
		return instance;
	}
}
  • 2 饿汉式(静态代码块)
代码语言:javascript复制
package com.shi.design.singleton.type2;

/**
 * 单例模式:2 饿汉式(静态代码块)
 * @author shiye
 *
 */
public class Singleton2 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  2. 再静态代码块中创建对象实例
 *  3. 提供一个共有的静态方法,返回实力对象
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static  Singleton instance;
	
	static {
		instance= new Singleton();
	}
	
	public static Singleton getInstance() {
		return instance;
	}
}
  • 3 懒汉式式(线程不安全)
代码语言:javascript复制
package com.shi.design.singleton.type3;

/**
 * 单例模式:3 懒汉式式(线程不安全)
 * @author shiye
 *
 */
public class Singleton3 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  23. 提供一个共有的静态方法,再需要的时候去创建找个对象
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static  Singleton instance;
	
	public static Singleton getInstance() {
		if(instance == null) {
			instance= new Singleton();
		}
		return instance;
	}
}
  • 4 懒汉式式(线程安全)
代码语言:javascript复制
package com.shi.design.singleton.type4;

/**
 * 单例模式:4 懒汉式式(线程安全)
 * @author shiye
 *
 */
public class Singleton4 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  23. 提供一个共有的静态方法,再需要的时候去创建找个对象 (加锁synchronized 解决线程安全的问题)
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	private static  Singleton instance;
	
	public static synchronized Singleton getInstance() {
		if(instance == null) {
			instance= new Singleton();
		}
		return instance;
	}
}
  • 5 懒汉式式(线程安全,双重检查 -推荐使用)
代码语言:javascript复制
package com.shi.design.singleton.type5;

/**
 * 单例模式:5 懒汉式式(线程安全,双重检查)
 * @author shiye
 *
 */
public class Singleton5 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  23. 提供一个共有的静态方法,再需要的时候去创建找个对象
 * (加线程代码块synchronized 解决线程安全的问题,并且进行双重检查)
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	//volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。
	private static volatile Singleton instance;
	
	public static Singleton getInstance() {
		if(instance == null) {
			synchronized (Singleton.class) {
				if(instance == null) {
					instance= new Singleton();
				}
			}
		}
		return instance;
	}
}
  • 6 懒加载式(静态内部类 推荐使用)
代码语言:javascript复制
package com.shi.design.singleton.type6;

/**
 * 单例模式:6 懒加载式(静态内部类)
 * @author shiye
 *
 */
public class Singleton6 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.getInstance();
		Singleton singleton2 = Singleton.getInstance();
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
	}
}

/**
 *  1. 构造器私有化,外部不能new
 *  2. 创建一个静态内部类(Singleton 类装载到内存中不会装载SingletonInstance类)
 *  3. 提供一个getInstance()方法:
 *  当getInstance()方法被调用的时候才回去实例化SingletonInstance类(装载类是线程安全的)
 * @author shiye
 *
 */
class Singleton{
	private Singleton(){}
	
	public static class SingletonInstance{
		public static final Singleton INSTANCE = new Singleton();
	}
	
	public static Singleton getInstance() {
		return SingletonInstance.INSTANCE;
	}
}
  • 7 使用枚举 (推荐使用)
代码语言:javascript复制
package com.shi.design.singleton.type7;

/**
 * 单例模式:7 使用枚举
 * @author shiye
 *
 */
public class Singleton7 {

	public static void main(String[] args) {
		Singleton singleton1 = Singleton.INSTANCE;
		Singleton singleton2 = Singleton.INSTANCE;
		
		System.out.println(singleton1 == singleton2);//true
		System.out.println("singleton1.hashCode() = "   singleton1.hashCode());
		System.out.println("singleton2.hashCode() = "   singleton2.hashCode());
		
		singleton1.say();
		singleton2.say();
	}
}

/**
 *  使用枚举
 * @author shiye
 *
 */
enum Singleton{
	INSTANCE;
	public void say() {
		System.out.println("hello ~ ");
	}
}

0 人点赞