We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Consider static factory methods instead of constructors

gudii9
gudii9 asked
on
Medium Priority
358 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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);
Awarded 2011
Awarded 2011

Commented:

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

-----------------------------------------------------------------

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

Author

Commented:
>>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
Awarded 2011
Awarded 2011

Commented:

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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Consider static factory methods instead of constructors

which is what I have showed you, a "static factory method"
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.