Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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
Avatar of for_yan
for_yan
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
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);

This is correction of small but significant mistake in my previous post
------------------------------------------------------------
should be:

MySingletonClass   msc = MySingletonClass.getMySingleton();

instead of

MySingletonClass   msc = MySingletonClass.getInstance();

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.getMySingleton();

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).

-----------------------------------------------------------------
Avatar of mbmast
mbmast

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.getCatInstance();

OR

Dog myDog = AnimalFactory.getDogInstance();

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).
Avatar of gudii9

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

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> Consider static factory methods instead of constructors

which is what I have showed you, a "static factory method"