Singleton and Lazyloading

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单例模式(Singleton Pattern)是一种创建型模式,它保证一个类只有一个实例,并提供一个全局访问点。单例模式通常用于控制某些资源的访问权限,或者在整个系统中只需要一个共享资源的情况下使用。 在单例模式中,类的构造函数必须私有化,这样外部就不能直接实例化该类。同时,类中需要定义一个静态方法,用于获取该类唯一的实例。在第一次调用该方法时,会创建一个实例,并将其保存下来。以后每次调用该方法时,都会返回同一个实例。 例如,下面的代码演示了如何在C++中实现单例模式: ```c++ class Singleton { public: static Singleton& getInstance() { static Singleton instance; // 延迟初始化,保证线程安全 return instance; } private: Singleton() {} // 将构造函数私有化,防止外部实例化 Singleton(const Singleton&) = delete; // 禁止拷贝构造函数 Singleton& operator=(const Singleton&) = delete; // 禁止赋值运算符 }; int main() { Singleton& s1 = Singleton::getInstance(); Singleton& s2 = Singleton::getInstance(); std::cout << std::boolalpha << (&s1 == &s2) << std::endl; // 输出:true return 0; } ``` 在这个例子中,我们定义了一个名为Singleton的类,并将其构造函数私有化,防止外部实例化。同时,我们定义了一个静态方法getInstance,用于获取该类唯一的实例。在getInstance方法中,我们使用了静态局部变量的方式来延迟初始化,保证线程安全。最后,在main函数中,我们多次调用getInstance方法,每次调用都会返回同一个实例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值