欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

singleton pattern(单例模式)

程序员文章站 2022-07-14 07:56:53
...

        在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在。这样的模式有几个好处:

1、某些类创建比较频繁,对于一些大型的对象,这是一笔很大的系统开销。

2、省去了new操作符,降低了系统内存的使用频率,减轻GC压力。

 

         In JAVA applications, the application of SINGLETON PATTERN can ensure that only one instance exists in one JVM. There are several advantages, listed as below.

1, It is frequent to create instances of some classes, so to some large objects, it may be a big overhead for system.

2,It can discard NEW operator, reduce the system momery usage frequency and the pressure of GC.

 

常见的几种创建单例模式如下:

The followings are several common manners to create singleton objects, listed below:

 

方式一:(manner one)

 

package com.zhuweiwei.usage.singleton; 
  
public final class SingletonOne { 

    private static SingletonOne instance = null; 

    private SingletonOne() {} 

    public static synchronized SingletonOne newInstance() 
    {
        return instance == null ? instance = new SingletonOne() : instance; 
    }

}

 

方式二:

 

package com.zhuweiwei.usage.singleton; 

public final class SingletonTwo { 

    private static final SingletonTwo instance = new SingletonTwo(); 

    private SingletonTwo() {} 

    public static SingletonTwo newInstance() 
    {
        return instance; 
    }

}

 

 

方式三:

package com.zhuweiwei.usage.singleton; 

public final class SingletonThree { 

    private static SingletonThree instance = null; 

    private SingletonThree() {} 

    public static SingletonThree newInstance() 
    {
         return instance == null ? createInstance() : instance; 
    }

    private static synchronized SingletonThree createInstance() 
    {
         return instance = new SingletonThree(); 
    }

}

  

方式四:

package com.zhuweiwei.usage.singleton; 

public final class SingletonFour { 

    private SingletonFour() {} 

    public static SingletonFour newInstance() 
    {    
        return SingletonFourFactory.instance; 
    }

    private static class SingletonFourFactory { 
        private static final SingletonFour instance = new SingletonFour(); 
    }

}

  

测试:(test case)

package com.zhuweiwei.usage.singleton; 

public class SingletonTest { 

    public static void main(String[] args) 
    {
        SingletonOne object1 = SingletonOne.newInstance();
        SingletonOne object2 = SingletonOne.newInstance();
        SingletonOne object3 = SingletonOne.newInstance();
        System.out.println((object1 == object2) && (object1 == object3) && (object2 == object3)); 
 
        SingletonTwo object4 = SingletonTwo.newInstance();
        SingletonTwo object5 = SingletonTwo.newInstance();
        SingletonTwo object6 = SingletonTwo.newInstance();
        System.out.println((object4 == object5) && (object5 == object6) && (object4 == object6)); 

        SingletonThree object7 = SingletonThree.newInstance();
        SingletonThree object8 = SingletonThree.newInstance();
        SingletonThree object9 = SingletonThree.newInstance();
        System.out.println((object7 == object8) && (object8 == object9) && (object7 == object9)); 

        SingletonFour object10 = SingletonFour.newInstance();
        SingletonFour object11 = SingletonFour.newInstance();
        SingletonFour object12 = SingletonFour.newInstance();
        System.out.println((object10 == object11) && (object11 == object12) && (object10 == object12));
    }

}

 

上一篇: C

下一篇: 单例模式Singleton Pattern