单例设计模式的4种实现方式


       所谓单例设计模式简单说就是无论程序如何运行,采用单例设计模式的类(Singleton类)永远只会有一个实例化对象产生。具体实现步骤如下:

      (1) 将采用单例设计模式的类的构造方法私有化(采用private修饰)。

      (2) 在其内部产生该类的实例化对象,并将其封装成private static类型。

      (3) 定义一个静态方法返回该类的实例。

         示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class  Singleton {  
     private  static  Singleton instance =  new  Singleton(); // 在内部产生本类的实例化对象 
   
     public  static  Singleton getInstance() {  // 通过静态方法返回instance对象 
         return  instance;  
     }  
   
     private  Singleton() {  // 将构造方法封装为私有化 
     }  
   
     public  void  print() {  
         System.out.println( "Hello World!!!" );  
     }  
}  
   
public  class  SingletonDemo {  
     public  static  void  main(String args[]) {  
         Singleton s1 =  null // 声明对象 
         Singleton s2 =  null // 声明对象 
         Singleton s3 =  null // 声明对象 
         s1 = Singleton.getInstance();  // 取得实例化对象 
         s2 = Singleton.getInstance();  // 取得实例化对象 
         s3 = Singleton.getInstance();  // 取得实例化对象 
         s1.print();  // 调用方法  
         s2.print();  // 调用方法  
         s3.print();  // 调用方法  
     }  

   一、单例模式的介绍 
     Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点 

二、单例模式的实现 

实现的方式有如下四种: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** 
  *  
  * 单例模式的实现:饿汉式,线程安全 但效率比较低 
  */ 
public  class  SingletonTest {  
   
     private  SingletonTest() {  
     }  
   
     private  static  final  SingletonTest instance =  new  SingletonTest();  
   
     public  static  SingletonTest getInstancei() {  
         return  instance;  
     }  
   

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** 
  * 单例模式的实现:饱汉式,非线程安全  
  *  
  */ 
public  class  SingletonTest {  
     private  SingletonTest() {  
     }  
   
     private  static  SingletonTest instance;  
   
     public  static  SingletonTest getInstance() {  
         if  (instance ==  null )  
             instance =  new  SingletonTest();  
         return  instance;  
     }  

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/** 
  * 线程安全,但是效率非常低 
  * @author vanceinfo 
 
  */ 
public  class  SingletonTest {  
     private  SingletonTest() {  
     }  
   
     private  static  SingletonTest instance;  
   
     public  static  synchronized  SingletonTest getInstance() {  
         if  (instance ==  null )  
             instance =  new  SingletonTest();  
         return  instance;  
     }  

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** 
  * 线程安全  并且效率高 
 
  */ 
public  class  SingletonTest {  
     private  static  SingletonTest instance;  
   
     private  SingletonTest() {  
     }  
   
     public  static  SingletonTest getIstance() {  
         if  (instance ==  null ) {  
             synchronized  (SingletonTest. class ) {  
                 if  (instance ==  null ) {  
                     instance =  new  SingletonTest();  
                 }  
             }  
         }  
         return  instance;  
     }  

  

猜你喜欢

转载自blog.csdn.net/tail_0701/article/details/79800700