赞
踩
所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类.只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。
比如Hibernate的SessionFactory,它充当数据存储源的代理,并负责创建Session对象。SessionFactory并不是轻量级的,一般情况下,一个项目通常只需要一个SessionFactory就够,这是就会使用到单例模式。
- public class SingletonTest01 {
-
- public static void main(String[] args) {
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 饿汉式(静态变量) // 线程安全
- class Singleton {
- // 1.构造器私有化,外部能new
- private Singleton(){
-
- }
-
- // 2在类内部创建对象实例
- private final static Singleton instance = new Singleton();
-
- // 提供一个公有的静态方法
- public static Singleton getInstance(){
- return instance;
- }
- }

- public class SingletonTest02 {
-
- public static void main(String[] args) {
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 饿汉式(静态变量) 线程安全
- class Singleton {
- // 1.构造器私有化,外部能new
- private Singleton(){
-
- }
-
-
- // 2。在类内部创建对象实例
- private static Singleton instance;
-
- // 在静态代码块中创建单例对象
- static {
- instance = new Singleton();
- }
-
- // 提供一个公有的静态方法
- public static Singleton getInstance(){
- return instance;
- }
- }

- public class SingletonTest03 {
-
- public static void main(String[] args) {
- System.out.println("懒汉式1,线程不安全");
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 懒汉式 线程不安全
- class Singleton {
- private static Singleton instance;
- private Singleton() {}
-
- // 提供一个静态的公有方法,当使用到该方法时,才去创建instance
- // 即懒汉式
- public static Singleton getInstance(){
- if (instance == null){
- instance = new Singleton();
- }
- return instance;
- }
- }

- public class SingletonTest04 {
-
- public static void main(String[] args) {
- System.out.println("懒汉式2,线程安全");
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 懒汉式(线程安全,同步方法)
- class Singleton {
- private static Singleton instance;
- private Singleton() {}
-
- // 提供一个静态的公有方法,当使用到该方法时,才去创建instance,加入同步代码块,解决线程安全问题
- // 即懒汉式
- public static synchronized Singleton getInstance(){
- if (instance == null){
- instance = new Singleton();
- }
- return instance;
- }
- }

- public class SingletonTest05 {
-
- public static void main(String[] args) {
- System.out.println("懒汉式3,线程不安全");
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 懒汉式(线程不安全,同步方法)
- class Singleton {
- private static Singleton instance;
- private Singleton() {}
-
- // 提供一个静态的公有方法,当使用到该方法时,才去创建instance,加入同步代码块,解决线程安全问题
- // 即懒汉式
- public static Singleton getInstance(){
- if (instance == null){
- // 此时可能有多个线程在这等待,等锁释放还是会创建多个实例,所以线程不安全
- synchronized (Singleton.class){
- instance = new Singleton();
- }
- }
- return instance;
- }
- }

- public class SingletonTest06 {
-
- public static void main(String[] args) {
- System.out.println("双重检查锁");
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 懒汉式(线程安全,同步方法)
- class Singleton {
- private static volatile Singleton instance;
- private Singleton() {}
-
- // 提供一个静态的公有方法,当使用到该方法时,才去创建instance,加入双重检查锁,加入同步代码块,解决线程安全问题
- // 同时效率较高
- // 即懒汉式
- public static Singleton getInstance(){
- // 双重检查锁
- if (instance == null){
- synchronized (Singleton.class){
- if (instance == null){
- instance = new Singleton();
- }
- }
- }
- return instance;
- }
- }

- public class SingletonTest07 {
-
- public static void main(String[] args) {
- System.out.println("使用静态内部类完成单例模式");
- // 测试
- Singleton instance = Singleton.getInstance();
- Singleton instance1 = Singleton.getInstance();
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
- }
-
- }
-
- // 静态内部类完成
- class Singleton {
- // 构造器私有化
- private Singleton() {}
-
- // 写一个静态内部类,该类中有一个静态属性 Singleton
- private static class SingletonInstance {
- private static final Singleton INSTANCE = new Singleton();
- }
-
- // 提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
- public static Singleton getInstance(){
- return SingletonInstance.INSTANCE;
- }
- }

- public class SingletonTest08 {
-
- public static void main(String[] args) {
- Singleton instance = Singleton.INSTANCE;
- Singleton instance1 = Singleton.INSTANCE;
- // true 表示两个对象为同一个引用
- System.out.println(instance1 == instance);
- System.out.println("instance.hashCode() = " + instance.hashCode());
- System.out.println("instance1.hashCode() = " + instance1.hashCode());
-
- instance.sayOK();
- }
-
- }
-
- // 使用枚举,可以实现单例,推荐
- enum Singleton {
- INSTANCE; // 属性
- public void sayOK(){
- System.out.println("ok~~");
- }
- }

JDK中,java.lang.Runtime就是经典的单例模式(饿汉式)
因为Runtime创建了是一定会被使用的,所以可以采取饿汉式
- public class Runtime {
- private static Runtime currentRuntime = new Runtime();
-
- /**
- * Returns the runtime object associated with the current Java application.
- * Most of the methods of class <code>Runtime</code> are instance
- * methods and must be invoked with respect to the current runtime object.
- *
- * @return the <code>Runtime</code> object associated with the current
- * Java application.
- */
- public static Runtime getRuntime() {
- return currentRuntime;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。