Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective Java By Joshua Bloch: Item1 - Static Factory Method

Tags:

I am reading the Effective Java by Joshua Bloch and I have question about Item1 Static Factory Method.

Quote[Bloch, p.7]

Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types. For example, the Java Collections Framework, provide unmodifiable collections, synchronized collections, and the like. Nearly all of these implementations are export via static factory methods in one noninstantiable class (java.util.Collections). The classes of the returned objects are all non-public.

Ok. When look at the sources code, I see java.util.Collection interface and java.util.Collections class with private constructor (non-instantiable class). and I see that the non-instantiable class Collections has all static methods, just like what Bloch said. But i fail to see the connection between the two classes as Bloch said

Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types.

  1. Can anyone point out the obvious to me?

  2. what is it mean when he said

The classes of the returned objects are all non-public

Here is where I obtain the java sources: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collection.java?av=f

like image 830
Thang Pham Avatar asked May 25 '11 18:05

Thang Pham


People also ask

What is static factory method in Java?

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.

Which method use static factory method design pattern in JDK?

Example of a static factory method in JDKvalueOf() method which returns object created by factory equivalent to value of parameter passed. getInstance() method which creates instance of Singleton class. newInstance() method which is used to create and return new instance from factory method every time called.

Which of the following options is a disadvantage of using static factory method?

The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.

Should a factory method 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.


1 Answers

  1. Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types.

    The point is just the plural 's' on "Type[s]". So if your interface is called Foo and you want to create some implementation called MyFoo then your factory with the methods to instantiate should be called Foos by convention.

  2. The classes of the returned objects are all non-public

    This means that the classes of objects returned from the factory methods have a private or default visibility modifier as in private class MyFoo{} so that they can not be instantiated by any other means but their factory methods. Since you can't construct an Object using the new operator from private inner or package private class out of their scope (reflection aside).

e.g.:

 public interface Foo{ //interface without plural 's' (question 1)      public void bar();  }  public abstract class Foos(){ // abstract factory with plural 's' (question 1)     public static Foo createFoo(){         return new MyFoo();     }     private class MyFoo implements Foo{ // a non visible implementation (question 2)        public void bar(){}     }  } 
like image 93
fasseg Avatar answered Sep 18 '22 15:09

fasseg