Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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
0
gudii9
Asked:
gudii9
2 Solutions
 
for_yanCommented:

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

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


0
 
objectsCommented:
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);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:

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

-----------------------------------------------------------------
0
 
mbmastCommented:
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).
0
 
gudii9Author 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
0
 
for_yanCommented:

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
0
 
objectsCommented:
> It looks like regular method calling not calling factory specific unique instance calling right. please advise

Its not a regular method, its a *static* method
And its returning you an instance without needing to call the constructor directly which is what your question is asking about

factory methods have nothing to with unique instance and singletons, that is a completely unrelated area
0
 
objectsCommented:
> Consider static factory methods instead of constructors

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now