java,单例设计模式

  • 单例设计模式(饿汉式)

 

Java代码   收藏代码
  1. class Singleton {   // 编写一个普通类  
  2.     private static final Singleton INSTANCE = new Singleton() ;  
  3.     private Singleton() {}   // 构造方法私有了  
  4.     public static Singleton getInstance() {  
  5.         return INSTANCE ;  
  6.     }  
  7.     public void print() {  
  8.         System.out.println("www.baidu.com") ;  
  9.     }  
  10. }  
  11. public class TestDemo {  
  12.     public static void main(String args[]) {  
  13.         Singleton inst = null ; // 声明对象  
  14.         inst = Singleton.getInstance() ;  
  15.         inst.print() ;  
  16.     }  
  17. }  

           构造方法私有化,外部无法产生新的实例化对象,只能够通过类提供的static方法取得唯一的一个对象的引用。

 

      对于单例设计模式有两类:饿汉式(以上代码)、懒汉式。

       · 饿汉式:不管程序中是否有对象需要使用此类,那么此类的对象都实例化好;

       · 懒汉式:在第一次使用的时候才进行实例化。

  • 范例下载:观察懒汉式

 

 

懒汉式代码   收藏代码
  1. class Singleton {   // 编写一个普通类  
  2.     private static Singleton instance  ;  
  3.     private Singleton() {}   // 构造方法私有了  
  4.     public static Singleton getInstance() {  
  5.         if (instance == null) {  
  6.             instance = new Singleton() ;    // 需要的时候进行实例化  
  7.         }  
  8.         return instance ;  
  9.     }  
  10.     public void print() {  
  11.         System.out.println("www.mldn.cn") ;  
  12.     }  
  13. }  
  14. public class TestDemo {  
  15.     public static void main(String args[]) {  
  16.         Singleton inst = null ; // 声明对象  
  17.         inst = Singleton.getInstance() ;  
  18.         inst.print() ;  
  19.     }  
  20. }  
 

 

  •  多例设计模式

         不管是单例设计还是多例设计,本质就一个:构造方法私有化,内部产生实例化对象,只不过单例设计只产生一个,多例设计会产生多个。

    例如:现在要求描述一周时间数的类,只能够有七个对象;

    例如:例如要求描述性别的类,只能有两个。

范例下载性别的描述

多例设计模式代码   收藏代码
  1. class Sex {  
  2.     public static final int MALE_CH = 1 ;  
  3.     public static final int FEMALE_CH = 2 ;  
  4.     private static final Sex MALE = new Sex("男") ;  
  5.     private static final Sex FEMALE = new Sex("女") ;  
  6.     private String title ;  
  7.     private Sex(String title) {  
  8.         this.title = title ;  
  9.     }  
  10.     public static Sex getInstance(int ch) {  
  11.         switch(ch) {  
  12.             case MALE_CH :  
  13.                 return MALE ;  
  14.             case FEMALE_CH :  
  15.                 return FEMALE ;  
  16.             default :  
  17.                 return null ;  
  18.         }  
  19.     }  
  20.     public String toString() {  
  21.         return this.title ;  
  22.     }  
  23. }  
  24. public class TestDemo {  
  25.     public static void main(String args[]) {  
  26.         Sex sex = Sex.getInstance(Sex.MALE_CH) ;  
  27.         System.out.println(sex) ;  
  28.     }  
  29. }  

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值