【设计模式】之实例化单例模式

  1. com.cleversoft.designpatterns.singleton1.Singleton.java:
  2. package com.cleversoft.designpatterns.singleton1;
  3. /** *//**
  4.  * Hungry Singleton
  5.  * 
  6.  */
  7. public class Singleton {
  8.     private static Singleton singleton = new Singleton();
  9.     private Singleton() {
  10.     }
  11.     public static Singleton getInstance() {
  12.         return singleton;
  13.     }
  14.     public String demoMethod() {
  15.         return "This is a hungry singleton demo!";
  16.     }
  17. }
  18. //---------------------------------------------------------------------------------------------------------
  19. com.cleversoft.designpatterns.singleton1.Main.java:
  20. package com.cleversoft.designpatterns.singleton1;
  21. public class Main {
  22.     /** *//**
  23.      * @param args
  24.      */
  25.     public static void main(String[] args) {
  26.         // TODO Auto-generated method stub
  27.         System.out.println(Singleton.getInstance().demoMethod());
  28.     }
  29. }
  30. //---------------------------------------------------------------------------------------------------------
  31. 懒汉式单例
  32. com.cleversoft.designpatterns.singleton2.LazySingleton.java:
  33. package com.cleversoft.designpatterns.singleton2;
  34. /** *//**
  35.  * Lazy Singleton
  36.  * 
  37.  */
  38. public class LazySingleton {
  39.     static boolean instance_flag = false;
  40.     private LazySingleton() {
  41.     }
  42.     synchronized public static LazySingleton getInstance() {
  43.         if (!instance_flag) {
  44.             instance_flag = true;
  45.             return new LazySingleton();
  46.         } else
  47.             return null;
  48.     }
  49. }
  50. //---------------------------------------------------------------------------------------------------------
  51. com.cleversoft.designpatterns.singleton2.Main.java:
  52. package com.cleversoft.designpatterns.singleton2;
  53. public class Main {
  54.     /** *//**
  55.      * @param args
  56.      */
  57.     public static void main(String[] args) {
  58.         // TODO Auto-generated method stub
  59.         LazySingleton ls1, ls2;
  60.         System.out.println("Start to get first Instance");
  61.         ls1 = LazySingleton.getInstance();
  62.         if (ls1 != null){
  63.             System.out.println("This is the first Instance!/n");
  64.         }
  65.         
  66.         System.out.println("Start to get second Instance");
  67.         ls2 = LazySingleton.getInstance();
  68.         if (ls2 != null){
  69.             System.out.println("This is the second Instance!");
  70.         }
  71.         else{            
  72.             System.out.println("You can just get 1 Instance!");
  73.         }
  74.         
  75.     }
  76. }
  77. 引用:
  78.  >强烈建议大家不要再花时间在实现双重检查成例上
  79. 是这样,不必在这上面钻牛角尖,而且单例模式在实际应用中已经逐步被Ioc容器替代,实用价值不高。

 

  1. 有上限多例模式
  2. com.cleversoft.designpatterns.multiton.Die.java:
  3. package com.cleversoft.designpatterns.multiton;
  4. import java.util.Random;
  5. import java.util.Date;
  6. public class Die {
  7.     private static Die die1 = new Die();
  8.     private static Die die2 = new Die();
  9.     private Die() {
  10.     }
  11.     public static Die getInstance(int whichOne) {
  12.         if (whichOne == 1) {
  13.             return die1;
  14.         } else {
  15.             return die2;
  16.         }
  17.     }
  18.     public synchronized int dice() {
  19.         Date d = new Date();
  20.         Random r = new Random(d.getTime());
  21.         int value = r.nextInt();
  22.         value = Math.abs(value);
  23.         value = value % 6;
  24.         value += 1;
  25.         System.out.println(value);
  26.         return value;
  27.     }
  28. }
  29. //---------------------------------------------------------------------------------------------------------
  30. com.cleversoft.designpatterns.multiton.Main.java:
  31. package com.cleversoft.designpatterns.multiton;
  32. public class Main {
  33.     private static Die die1;
  34.     private static Die die2;
  35.     public static void main(String[] args) {
  36.         die1 = Die.getInstance(1);
  37.         die2 = Die.getInstance(2);
  38.         die1.dice();
  39.         die2.dice();
  40.     }
  41. }
  42. 无上限多例模式
  43. 比较合适的例子就是在应用程序中的“多语言支持”和序列键生成器。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值