gudii9
asked on
Consider static factory methods instead of constructors
Hi,
I was reading like
'Consider static factory methods instead of constructors'
I did not understand what it mean.
Please advise on how can i resolve it. Any ideas, sample code, links, resources highly appreciated. thanks in advance
I was reading like
'Consider static factory methods instead of constructors'
I did not understand what it mean.
Please advise on how can i resolve it. Any ideas, sample code, links, resources highly appreciated. thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
public MyClass {
// constructor
public MyClass(in x) {}
...
}
// static factory method
public static MyClass getMyClass(int am, int b) {
...
}
}
Usage would be:
MyClass a = new MyClass(4);
MyClass b = MyClass.getMyClass(3, 4);
// constructor
public MyClass(in x) {}
...
}
// static factory method
public static MyClass getMyClass(int am, int b) {
...
}
}
Usage would be:
MyClass a = new MyClass(4);
MyClass b = MyClass.getMyClass(3, 4);
This is correction of small but significant mistake in my previous post
--------------------------
should be:
MySingletonClass msc = MySingletonClass.getMySing
instead of
MySingletonClass msc = MySingletonClass.getInstan
Method is my example was getMySingleton();
although getInstance() is a commnon name for such methods
in these clsses - that's why I made this mistake
--------------------------
corrected previous post:
--------------------------
Probably most important case when you use factory methiods is in case of singletons - where
you want onluy one instance of your class to exist in any application.
In such case instead of providing public constructor like here:
public class MyClass() {
public MyClass(){
}
}
This one anyone in their application can use like that
MyClass mc = new MyClass();
and anyone can create any number of them:
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
etc.
In some cases you want that the instance of your class sjhould be unique
in the application then you can do it this way:
public MySingletonClass {
MySingletonClass insrtance;
private MySungletonClass(){
}
static MySingletonClass getMySingleon(){
if(instance != null) return instance;
else
{
instance = MySungletonClass();
return instance;
}
}
}
In this case someone who wants ti use your ckass
in his code will have to do
MySingletonClass msc = MySingletonClass.getMySing
and if he tries to do this second time,
he'll again receove the same
instance of the class.
Somertimes it may be quite useful especuially
if class is develiped by one persion and used
by different programmer(s).
--------------------------
More generally, you may need a specific instance of a derived class. You can ask the factory, via unique method calls, to return specific instances. For example, you may have a general Animal interface that is implemented by two derived classes, Dog and Cat.
Cat myCat = AnimalFactory.getCatInstan ce();
OR
Dog myDog = AnimalFactory.getDogInstan ce();
The factory can return the same Dog or Cat object over and over again, or it can create new instances each time or it can pull them out of a cache that contains a limited number of instances (this would be appropriate when each instance consumes a lot of resources and you want to limit the resources that are consumed at only one time).
Cat myCat = AnimalFactory.getCatInstan
OR
Dog myDog = AnimalFactory.getDogInstan
The factory can return the same Dog or Cat object over and over again, or it can create new instances each time or it can pull them out of a cache that contains a limited number of instances (this would be appropriate when each instance consumes a lot of resources and you want to limit the resources that are consumed at only one time).
ASKER
>>public MyClass {
// constructor
public MyClass(in x) {}
...
}
// static factory method
public static MyClass getMyClass(int am, int b) {
...
}
}
Usage would be:
MyClass a = new MyClass(4);
MyClass b = MyClass.getMyClass(3, 4);
i was not clear on above post. It looks like regular method calling not calling factory specific unique instance calling right. please advise
// constructor
public MyClass(in x) {}
...
}
// static factory method
public static MyClass getMyClass(int am, int b) {
...
}
}
Usage would be:
MyClass a = new MyClass(4);
MyClass b = MyClass.getMyClass(3, 4);
i was not clear on above post. It looks like regular method calling not calling factory specific unique instance calling right. please advise
You are right regular methods and constructor are not static factory methods.
With public constructor we usually
can create as many instances of our class as we want.
The factory method could insure that if one instance is created, any subsequent
request for the instance of certain class
would receive the same instance, rather than cnew one would be created -
thsi is one of the important difference between public construuctor creation of object vs the
way of getting instances of objectsusing static method from factory
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> Consider static factory methods instead of constructors
which is what I have showed you, a "static factory method"
which is what I have showed you, a "static factory method"
You can read this explanation:
http://wowjava.wordpress.com/2009/01/09/providing-static-factory-methods-instead-of-constructors/