Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instance factory methods Vs Static factory methods

Can't all factory methods be static ? Does something that produces a product need state ? When is it appropriate to go for a instance factory or static factory method ? Can you provide me examples differentiating the two ?

like image 406
user2434 Avatar asked Nov 16 '11 05:11

user2434


People also ask

What is instance factory method?

A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.

Which are the three types of factory method?

the abstract factory pattern,the static factory method, the simple factory (also called factory).

When would you use a static factory method?

A static factory method is a public static method on the object that returns a new instance of the object. These type of methods share the same benefits as the traditional factory method design pattern. This is especially useful for value objects that don't have a separate interface and implementation class.

Should factory methods be static?

Specifically with the Factory pattern, no, there is no requirement that the factory methods be static. The essence of the pattern is that you have one object which is responsible for creating instances of another class.


Video Answer


2 Answers

Assuming that by "instance factory method" you're actually saying about the GoF "factory method", the term "static factory method" is described by Joshua Bloch in his book "Effective Java". Googling around I reached at these sites:

  • Factory Method: http://sourcemaking.com/design_patterns/factory_method
  • Static Factory Method: http://www.informit.com/articles/article.aspx?p=1216151

Hope that it helped to make the difference a little bit clearer.

Following Marvo's advice:

  • Factory Method as stated in GoF:

    define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Example (Java code):

    public abstract class Product { ... }      public class ConcreteProduct extends Product { ... }      public abstract class Creator {       public void anOperation() { ... product = factoryMethod(); ... }       public abstract Product factoryMethod();     }      public class ConcreteCreator extends Creator {       public Product factoryMethod() { return new ConcreteProduct(); }     } 
  • Static Factory Method as stated in Effective Java:

    A class can provide a public static factory method, which is simply a static method that returns an instance of the class. (...) One advantage of static factory methods is that, unlike constructors, they have names. (...) A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked. (...) A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. (...) The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.

Example (Java code):

    public class Boolean {       ...       public static Boolean valueOf(boolean b) {         return b ? Boolean.TRUE : Boolean.FALSE;       }       ...     } 
like image 109
Ramon Chiara Avatar answered Sep 24 '22 22:09

Ramon Chiara


My current preference is to make factory methods not static for the sake of easier testing. You can't change a static factory method call at runtime, whereas if I could provide a factory implementation to the object then I could test it more thoroughly as I'm in more control of the context and object graph.

like image 35
Nate W. Avatar answered Sep 21 '22 22:09

Nate W.