单例模式的java实现

 1 public class Singleton {
 2     /**
 3      * @author hanxin
 4      * 标题:单例模式
 5      * 功能:控制某个类只能实例话一次,或者说最多只有一个实例化对象
 6      * 实现方式(有两种):一种是懒汉模式,一种是饿汉模式
 7      */
 8     //懒汉模式
 9     private static Singleton instance;
10     public static Singleton getInstance()
11     {
12         if(instance==null)
13         {
14             instance=new Singleton();
15         }
16         return instance;
17     }
18 }
1 public class Singleton {
2 
3     //饿汉模式
4     private static Singleton instatce2=new Singleton();
5     public static Singleton getInstance2()
6     {
7         return instatce2;
8     }
9 }

单例模式的优缺点,及适合何时使用,以后补充。

欢迎交流。

 

小小程序员--一直很安静的我。

posted on 2013-09-03 20:57  一直很安静的我  阅读(163)  评论(0编辑  收藏  举报

导航