What are static factory methods?

前端 未结 14 770
粉色の甜心
粉色の甜心 2020-11-22 06:08

What\'s a \"static factory\" method?

相关标签:
14条回答
  • 2020-11-22 06:25

    Readability can be improved by static factory methods:

    Compare

    public class Foo{
      public Foo(boolean withBar){
        //...
      }
    }
    
    //...
    
    // What exactly does this mean?
    Foo foo = new Foo(true);
    // You have to lookup the documentation to be sure.
    // Even if you remember that the boolean has something to do with a Bar
    // you might not remember whether it specified withBar or withoutBar.
    

    to

    public class Foo{
      public static Foo createWithBar(){
        //...
      }
    
      public static Foo createWithoutBar(){
        //...
      }
    }
    
    // ...
    
    // This is much easier to read!
    Foo foo = Foo.createWithBar();
    
    0 讨论(0)
  • 2020-11-22 06:25

    It all boils down to maintainability. The best way to put this is whenever you use the new keyword to create an object, you're coupling the code that you're writing to an implementation.

    The factory pattern lets you separate how you create an object from what you do with the object. When you create all of your objects using constructors, you are essentially hard-wiring the code that uses the object to that implementation. The code that uses your object is "dependent on" that object. This may not seem like a big deal on the surface, but when the object changes (think of changing the signature of the constructor, or subclassing the object) you have to go back and rewire things everywhere.

    Today factories have largely been brushed aside in favor of using Dependency Injection because they require a lot of boiler-plate code that turns out to be a little hard to maintain itself. Dependency Injection is basically equivalent to factories but allows you to specify how your objects get wired together declaratively (through configuration or annotations).

    0 讨论(0)
  • 2020-11-22 06:26

    One of the advantages that stems from Static factory is that that API can return objects without making their classes public. This lead to very compact API. In java this is achieved by Collections class which hides around 32 classes which makes it collection API very compact.

    0 讨论(0)
  • 2020-11-22 06:28

    static

    A member declared with the keyword 'static'.

    factory methods

    Methods that create and return new objects.

    in Java

    The programming language is relevant to the meaning of 'static' but not to the definition of 'factory'.

    0 讨论(0)
  • 2020-11-22 06:33

    If the constructor of a class is private then you cannot create an object for class from outside of it.

    class Test{
     int x, y;
     private Test(){
      .......
      .......
      }
    }
    

    We cannot create an object for above class from outside of it. So you cannot access x, y from outside of the class. Then what is the use of this class?
    Here is the Answer : FACTORY method.
    Add the below method in above class

    public static Test getObject(){
      return new Test();
    }
    

    So now you can create an object for this class from outside of it. Like the way...

    Test t = Test.getObject();
    

    Hence, a static method which returns the object of the class by executing its private constructor is called as FACTORY method
    .

    0 讨论(0)
  • 2020-11-22 06:39
    • have names, unlike constructors, which can clarify code.
    • do not need to create a new object upon each invocation - objects can be cached and reused, if necessary.
    • can return a subtype of their return type - in particular, can return an object whose implementation class is unknown to the caller. This is a very valuable and widely used feature in many frameworks which use interfaces as the return type of static factory methods.

    fromhttp://www.javapractices.com/topic/TopicAction.do?Id=21

    0 讨论(0)
提交回复
热议问题