Lazy activation involves waiting to activate something until it is first used. It's similar to lazy instantiation where instead of creating an instance in advance, you wait until someone actually requests the instance.
For example:class Instantiator {
private Object eagerMember; // we'll use eager instantiation for this one
private Object lazyMember; // we'll use lazy instantiation for this one
public EagerInstatiator() {
// instantiate eager member as soon as this object is created
eagerMember = new String("Big string taking up memory and time upon creation of this");
}
public Object getEagerMember() {
// return immediately, the member is already available
return member1;
}
public Object getLazyMember() {
// instantiate lazyMember only when this method gets called for the first time
if(lazyMember == null) {
lazyMember = new String("Newly created string taking up memory and time upon first request");
}
return lazyMember;
}
}
Singletons are often lazily instatiated like this:class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {}
public static getInstance() {
if(instance == null) {
instance = new LazySingleton();
}
return instance;
}
I think that u r enforcing the Singleton type of situatin using
public Object getLazyMember() {
// instantiate lazyMember only when this method gets called for the first time
if(lazyMember == null) {
lazyMember = new String("Newly created string taking up memory and time upon first request");
}
Butall u have to check is wether " this method gets called for the first time" and for that y u check "lazyMember == null" offcousre only if the method if called for the first time the lazyMember will be null but here single ton kind of situation gets unnecessarily implemented .
As per ur idea lazy instantiation some thing which will happen only during first time methid call r can be said as "instanciated only wen needed" (some thing like late binding) but using if(lazyMember == null) makes it so that U can create only one instance.
Is this rite?
I'm not entirely sure what you mean. First of all there is no way to check if a method gets called for the first time. You need to keep track of this yourself. In my examples, I use the fact that I know lazyMember and instance will be null before the first call, and not null after. I could also have use a boolean to indicate that the method had not been called yet, but this way I have the added benefit of ensuring a non-null return value.
The first example does not implement a singleton pattern, just lazy instantiation of a member variable. The example use a single member variable that gets instantiated only once, otherwise it wouldn't be lazy instantiation, but just a method returning a new object every time.
> "The example use a single member variable that gets
> instantiated only once" But this wat a sigleton also
> does?
I'm sorry, I meant to say instance variable (or actually instance member). A singleton uses a class variable.
> so no difference am i right?
I generally use it for two reasons:
1. faster startup times. If you instantiate all the variables your program is ever going to need right away, it could take a long time for your application to start up. On a smaller scale: all object creation takes time, which is why you want to avoid creating objects you don't need right away.
2. memory usage. Instantiated objects take up space in memory. If you don't need an object right away (or at all), you save memory by not instantiating it.