singleton pattern(单例模式)

        在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));
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值