设计模式

单例模式

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

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

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

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

         示例代码如下:

 

Java代码 复制代码 收藏代码

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

  2. private static Singleton instance = new Singleton();// 在内部产生本类的实例化对象

  3.  
  4. public static Singleton getInstance() { // 通过静态方法返回instance对象

  5. return instance;

  6. }

  7.  
  8. private Singleton() { // 将构造方法封装为私有化

  9. }

  10.  
  11. public void print() {

  12. System.out.println("Hello World!!!");

  13. }

  14. }

  15.  
  16. public class SingletonDemo {

  17. public static void main(String args[]) {

  18. Singleton s1 = null; // 声明对象

  19. Singleton s2 = null; // 声明对象

  20. Singleton s3 = null; // 声明对象

  21. s1 = Singleton.getInstance(); // 取得实例化对象

  22. s2 = Singleton.getInstance(); // 取得实例化对象

  23. s3 = Singleton.getInstance(); // 取得实例化对象

  24. s1.print(); // 调用方法

  25. s2.print(); // 调用方法

  26. s3.print(); // 调用方法

  27. }

  28. }

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

二、单例模式的实现 

实现的方式有如下四种: 

Java代码 复制代码 收藏代码

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

  2. *

  3. * 单例模式的实现:饿汉式,线程安全 但效率比较低

  4. */

  5. public class SingletonTest {

  6.  
  7. private SingletonTest() {

  8. }

  9.  
  10. private static final SingletonTest instance = new SingletonTest();

  11.  
  12. public static SingletonTest getInstancei() {

  13. return instance;

  14. }

  15.  
  16. }

  17.  

 

Java代码 复制代码 收藏代码

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

  2. * 单例模式的实现:饱汉式,非线程安全

  3. *

  4. */

  5. public class SingletonTest {

  6. private SingletonTest() {

  7. }

  8.  
  9. private static SingletonTest instance;

  10.  
  11. public static SingletonTest getInstance() {

  12. if (instance == null)

  13. instance = new SingletonTest();

  14. return instance;

  15. }

  16. }

  17.  

 

Java代码 复制代码 收藏代码

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

  2. * 线程安全,但是效率非常低

  3. * @author vanceinfo

  4. *

  5. */

  6. public class SingletonTest {

  7. private SingletonTest() {

  8. }

  9.  
  10. private static SingletonTest instance;

  11.  
  12. public static synchronized SingletonTest getInstance() {

  13. if (instance == null)

  14. instance = new SingletonTest();

  15. return instance;

  16. }

  17. }

  18.  

 

Java代码 复制代码 收藏代码

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

 

工厂模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值