Singleton and Lazyloading

来源:互联网 发布:喷绘王类似软件 编辑:程序博客网 时间:2024/06/08 13:40
Well, thought it was a quite easy topic, unluckily, I was totally wrong and which makes me dizzy now!


Singleton

  Yeah, when it comes to singleton, either you write the singleton example following the common style, or you might give us a double checked lock. Not him, a Googler@CA,  Bob Lee!


First Edition which is seen everywhere:

public class Singleton 
    
static Singleton instance;

    
public static synchronized Singleton getInstance() {
        
if (instance == null)
            instance 
= new Singleton();
        
return instance;
    }

}

Second Edition which is usually called DCL (Double checked lock):

public class Singleton{
    
static volatile Singleton instance;
   
    
public static Singleton getInstance() {
        
if (instance == null{
            
synchronized (Singleton.class{
                
if (instance == null)
                    instance 
== new Singleton();
            }

        }

        
return instance;
    }

}

Bob's Edition:

public class Singleton{
    
static class SingletonHolder {
        
static Singleton instance = new Singleton(); 
    }

    
public static Singleton getInstance() {
        
return SingletonHolder.instance;
    }

}



Lazy Loading

    恩, 真正的故事开始了. 就识这个东西把我搞得晕晕的!一个最大问题是好多 谷歌 来资料都是E文的. 这倒不是头疼的开始, 让我感到晕倒的是各位资本主义国家的GG们的思维实在是发散, 各个角度满足了我探求Lazy的求知欲!!!从AOP到DDD, 从Proxy到Class Loader, 好容易见到国语 Edition的, 要么是简单成类似于Singleton实现, 要么就是讨论的问题根本就看不懂, 这一切都远远超出我的想象!

 基本概念

   以下是某位先生对Lazy Loading进行一个比较High Level的Introduction:

   Lazy loading, also known as dynamic function loading, is a mode that allows a developer to specify what components of a program should not be loaded into storage by default when a program is started. Ordinarily, the system loader automatically loads the initial program and all of its dependent components at the same time. In lazy loading, dependents are only loaded as they are specifically requested. Lazy loading can be used to improve the performance of a program if most of the dependent components are never actually used.

    A developer can enable lazy loading on a component-by-component basis in both threaded and non-threaded applications. The disadvantage of lazy loading is that if a component calls most of its dependents, every function call to a lazily loaded component requires extra instructions and time. Consequently, if a program can be expected to use most of its dependent components, then lazy loading will probably not improve performance.

好处

大概评估了一下,Lazy Loading有以下好处:
  1.提高运行时的响应度。执行三个SQL语句显然比执行一个SQL语句快。
  2.节省内存。一次性加载全部的依赖肯定会耗费更多的内存。
  3.不用担心循环引用。因为互相引用的两种不同的实体是在不同的时期加载的。
  4.对特殊的BLOB字段(例如较大的图片)似乎必须采用LazyLoad。

  PS 1: 关于第1点. Never meant anything, but I need to clear it more. 这里"执行三个SQL语句显然比执行一个SQL语句快"是指相对于每次进行get方法去要后台去做Connection后进行Query而言的, 即便使用了Connection Pool, 也是需要有好多后台操作, 何不一个Connection做多次Query呢
  PS 2: 关于第4点. 这种BLOB(CLOB)的数据可以通过在针对业务逻辑划分表格的时候, 可以将这种Fat 数据拆分出去, 成为一个独立的对象, 以方便的在需要的时候加载.

需要考虑的问题(Ud Dahan提出来的)

    原文提出问题大致如下:
    When working in a tiered deployment, client code needs to talk to some “service” (and I use the term lightly) to get the data. That same service may also contain a rich domain model which handles the business logic around updating its data. In this case, the lazy loading behavior of the domain model will be such as to bring the highest performance to the business logic of updating the data. The question then becomes, how do we retrieve that same data with lazy-loading behavior that will yield high performance or is suitable for serializing and sending back to the client.

    可以看得出来, 其本质还是那个经典的读写者问题. 就是在Lazy loading 的时候如何同步get进程和update进程.当然解决方法就多样了, 可以如Something about Cache一文第一节第6个问题介绍的锁机制, 可以利用操作系统的管程概念, 当然可以利用Udi Dahan先生自己介绍的(POJO/POCO/PONO Style)方法


   2篇推荐文章

  Peter Bell的ORM: Loading an Object (including aggregates and lazy loading)

  FransBouma的More on Lazy Loading vs. Pre-loading in O/R mapping scenarios


  几个没有看清楚的问题

  利用AOP来进行Lazy Load的方法

  Onjava上一篇04年的文章(要不是Onjava上面的, 我就滤掉这种04年骨灰级的文章了!), 其思想同Dynamic Proxy类似(恩, AOP不就可以通过Dynamic Proxy模拟吗), 在访问数据的时候, 通过横切关注点来动态加载数据

  Udi Dahan先生在DDD(Domain-Driven Design)方面的对Lazy loading的知识补充

  Johannes Brodwall在Java.net上面提出的Lazy Loading is Easy: Implementing a Rich Domain Model

   文章一开始就提出了3个问题
   1. When loading an object having a 1:1 relation. If the object on the other side of 1:1 relation is lazy loaded with <discriminator> tag, you will not be able to cast the returned object to the subclass you are expecting. That's because the proxy object returned is based on the method return type (which normaly match the abstract class).
   2. If lazy loaded object return no results, you would expect null to be returned. Instead a proxy object is returned and calling methods on the proxy object will return null.
   3. Its harder to debug cgi enhanced objects using GUI debuggers.
   第二个第三个问题不难理解, 对第一个问题我真的是不大明白.而且文章恰恰是利用Dynamic Proxy模型实行动态加载Class的方法来解决第一个问题的. 晕死了, 就是没有搞懂

   Kirill Grouchnikov先生从Class Loader的角度分析了Singleton
  同样是出自于java.net, 同样又是一篇没有看懂的文章,sigh, -_-b