设计模式-单例模式[Singleton]

本文为作者原创,转载请在明显位置标明出处:http://www.cnblogs.com/masque/p/3860746.html

有些对象其实我们只需要一个,比方说:线程池,缓存,日志对象.如果制造多个实例会产生程序异常.

方法一:

 1 package org.masque.designpatterns.singleton.one;
 2 /**
 3  * 
 4  * Description:
 5  * Singleton.java Create on 2014年7月22日 下午2:24:09 
 6  * @author masque.java@outlook.com
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Singleton {
11     /**
12      * 私有化无参构造方法,防止new 关键字创建对象
13      */
14     private Singleton(){}
15     
16     /**
17      * 1.5或更新的版本才支持volatile关键字
18      */
19     private volatile static Singleton singleton ;
20     
21     public static Singleton getInstance(){
22         if (singleton==null) {
23             synchronized (Singleton.class) {
24                 if (singleton==null) {
25                     singleton = new Singleton();
26                 }
27             }
28         }
29         return singleton;
30     }
31     
32     //省略其他方法
33 }

方法二:

 1 package org.masque.designpatterns.singleton.two;
 2 /**
 3  * 
 4  * Description:
 5  * Singleton.java Create on 2014年7月22日 下午2:24:09 
 6  * @author masque.java@outlook.com
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Singleton {
11     /**
12      * 私有化无参构造方法,防止new 关键字创建对象
13      */
14     private Singleton(){}
15     
16     private static Singleton singleton = new Singleton();
17     
18     public static Singleton getInstance(){
19         return singleton;
20     }
21     
22     //省略其他方法
23 }

方法三:

 1 package org.masque.designpatterns.singleton.three;
 2 /**
 3  * 
 4  * Description:
 5  * Singleton.java Create on 2014年7月22日 下午2:24:09 
 6  * @author masque.java@outlook.com
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Singleton {
11     /**
12      * 私有化无参构造方法,防止new 关键字创建对象
13      */
14     private Singleton(){}
15     
16     private static Singleton singleton ;
17     
18     public static synchronized Singleton getInstance(){
19         if (singleton==null) {
20             singleton = new Singleton();
21         }
22         return singleton;
23     }
24     
25     //省略其他方法
26 }

方法四:

 1 package org.masque.designpatterns.singleton.four;
 2 /**
 3  * 
 4  * Description:
 5  * Singleton.java Create on 2014年7月22日 下午2:24:09 
 6  * @author masque.java@outlook.com
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public enum Singleton {
11     /**
12      * 枚举类型只有一个实例
13      * (1.5或更新的版本才支持枚举)
14      */
15     SINGLETON;
16     
17     //省略其他方法
18 }

方法五:

 1 package org.masque.designpatterns.singleton.five;
 2 /**
 3  * 
 4  * Description:
 5  * Singleton.java Create on 2014年7月22日 下午2:24:09 
 6  * @author masque.java@outlook.com
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Singleton {
11     /**
12      * 私有化无参构造方法,防止new 关键字创建对象
13      */
14     private Singleton(){}
15     
16     private static Singleton singleton ;
17     
18     public static Singleton getInstance(){
19         /**
20          * 若并发执行 singleton==null 就会产生多个对象了
21          */
22         if (singleton==null) {
23             singleton = new Singleton();
24         }
25         return singleton;
26     }
27     
28     //省略其他方法
29 }

个人认为枚举是实现单利的最佳方式

posted on 2014-07-22 15:53  masque  阅读(275)  评论(0编辑  收藏  举报

导航