单列设计模式在实际开发中的一个应用

     在开发中经常用到select的标签将数据加载,供用户选择,我最近在开发一个跟帐务有关的应用系统,系统中包括了很多查

询功能,比如那款游戏进行查询,每次到查询页面,必须要显示所有游戏,为了避免每次打开该页面都要查询数据库的话那执

行效率就会很低。所以我写了一个缓存,简单解决这个问题。供大家查考!

java 代码
  1. public class SystemInfoCache {   
  2.   
  3.     private static Map map = null;   
  4.   
  5.     private static SystemInfoCache instance;   
  6.   
  7.     private static Object mutex = new Object();   
  8.   
  9.     /**  
  10.      * 单列设计模式保证应用中只有一个对象  
  11.      * @return  
  12.      */  
  13.     public static SystemInfoCache getInstance() {   
  14.   
  15.         if (instance == null) {   
  16.   
  17.             synchronized (mutex) {   
  18.   
  19.                 if (instance == null) {   
  20.   
  21.                     loadMap();   
  22.                     instance = new SystemInfoCache(map);   
  23.   
  24.                 }   
  25.   
  26.             }   
  27.   
  28.         }   
  29.         return instance;   
  30.   
  31.     }   
  32.   
  33.     /**  
  34.      * 通过id获取应用名称  
  35.      *   
  36.      * @param id 应用id对应数据中的主键  
  37.      * @return  
  38.      */  
  39.     public String getSysinfoName(Integer id) {   
  40.   
  41.         SystemInfoDTO dto = null;   
  42.         if (getInstance().map.containsKey(id)) {   
  43.   
  44.             dto = getInstance().map.get(id);   
  45.             return dto.getSysName();   
  46.         }   
  47.         try {   
  48.   
  49.             dto = BizLocator.getSystemInfoBiz().getSystemInfoById(id);   
  50.             return dto.getSysName();   
  51.   
  52.         } catch (DAOException e) {   
  53.   
  54.             e.printStackTrace();   
  55.   
  56.         }   
  57.         return "";   
  58.   
  59.     }   
  60.   
  61.     /**  
  62.      * 获得列表  
  63.      * @return 获得系统信息列表  
  64.      */  
  65.     public List<systeminfodto></systeminfodto> getList() {   
  66.   
  67.         if (instance.map == null)   
  68.             this.loadMap();   
  69.   
  70.         List<systeminfodto></systeminfodto> list = new ArrayList<systeminfodto></systeminfodto>();   
  71.         int size = instance.map.size();   
  72.         Iterator iter = instance.map.entrySet().iterator();   
  73.         for (int i = 0; i < size; i++) {   
  74.   
  75.             Map.Entry entry = (Map.Entry) iter   
  76.                     .next();   
  77.             list.add(entry.getValue());   
  78.         }   
  79.         return list;   
  80.   
  81.     }   
  82.   
  83.     /**  
  84.      * 加载数据中的数据  
  85.      *  
  86.      */  
  87.     private static void loadMap() {   
  88.   
  89.         map = new HashMap();   
  90.         try {   
  91.   
  92.             List<systeminfodto></systeminfodto> list = BizLocator.getSystemInfoBiz()//通过此方法获取数据中的应用系统信息   
  93.                     .getSystemInfoByName("");   
  94.             for (SystemInfoDTO dto : list) { //存储到map中   
  95.                 map.put(dto.getSysId(), dto);   
  96.             }   
  97.   
  98.         } catch (DAOException e) {   
  99.   
  100.             e.printStackTrace();   
  101.   
  102.         }   
  103.   
  104.     }   
  105.   
  106.     private SystemInfoCache(Map map) {   
  107.     }   
  108. }   

注:记录本人在开发中遇到的问题,与大家一起交流,如果有的好的解决方法,请指导一下,谢谢!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值