Static factory method in java class interface

I read the Effective java tutorial . The first element is about using static factory methods instead of a public constructor. I doubt that if I specify Interface, how can I specify a static factory method in Interface? Since java does not support static methods inside Interface. The text book describes how to create an uninteresting class that contains public static factory methods. But how can this method access the private constructor of the implementation class?

The text book says that if you define Interface Type, create a non-intuitive class Typesand include static factory methods in this class. But how Typesdoes a method defined in a class gain access to the private constructor of a specific implementationInterface Type

EDIT: - The sentence below is quoted in a textbook. Please explain to me its meaning.

"Interfaces cannot have static methods, therefore, by convention, the static factory methods for an interface named Type are placed in a non-integrable class (element 4) named" Types "

EDIT: - taken from Effective Java by Joshua Bloch: Item1 - Static factory Method

     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(){}
    }
 }

My question is: how does the static method createFoo()call the private constructorMyFoo

+4
4

factory , .

:

public Interface I { }

private class Impl implements I {
}

I buildI() {
    return new Impl();
}

, ( private) , factory .

, factory . , EnumSet factory Enum, EnumSet. - Enums 64 , .

factory, , , :

public interface Factory {
   I buildI();
}

setFactory(new FactoryImpl());, factory.buildI(), .

Generics:

public interface GenericFactory<T> {
    T buildInstance();
}

setFactory :

public void setFactory(GenericFactory<I> factory);

factory, :

public class FactoryImpl implements GenericFactory<I> {
     @override
     I buildInstance() {
        return new impl();
     }
}

factory , factory, .

, , - !

Java . , , , .

factory , , private, private.

+2

factory ,

public Interface MyInterface {
    public void myFunction();
}

public class MyClass implements MyInterface {

    // constructor is made private
    private MyClass() {}

    // Use this to create object instead use static factory
    public static MyClass getInstance() {
        return new MyClass();
    }

    public void myFunction(){
      // your implementation
    } 
}

factory. factory?

, , http://www.javacodegeeks.com/2013/01/static-factory-methods-vs-traditional-constructors.html

0

You cannot define a factory method in an interface, but you also cannot have a constructor in an interface: Interfaces cannot be created. The bottom line is to use the factory method instead of the constructor in classes that implement the interface, and not in the interface itself.

0
source

All Articles