Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

What is the benefit we get by using static factory method to create object?

Pragya Keshap answered on February 10, 2023 Popularity 2/10 Helpfulness 1/10

Contents


More Related Answers

  • java factory method pattern
  • c# factory method design pattern
  • factory function
  • Java Factory Object pattern
  • function(global factory)
  • explain factory method pattern briefly with example
  • Factory Methods in scala

  • What is the benefit we get by using static factory method to create object?

    0

    By using Static Factory Method we encapsulate the creation process

    of an object. We can use new() to create an Object from its

    constructor. Instead we use static method of a Factory to create the

    object. One main advantage of using Factory is that Factory can

    choose the correct implementation at runtime and create the right

    object. The caller of method can specify the desired behavior.

    E.g. If we have a ShapeFactory with createShape(String type)

    method. Client can call ShapeFactory.createShape(“Circle”) to get

    a circular shape. ShapeFactory.createShape(“Square”) will return

    square shape. In this way, ShapeFactory knows how to create

    different shapes based on the input by caller.

    Another use of Factory is in providing access to limited resources

    to a large set of users.

    E.g. In ConnectionPool, we can limit the total number of

    connections that can be created as well as we can hide the

    implementation details of creating connection. Here ConnectionPool

    is the factory. Clients call static method

    ConnectionPool.getConnection()

    https://www.baeldung.com/java-constructors-vs-static-factory-methods

    Popularity 2/10 Helpfulness 1/10 Language java
    Source: Grepper
    Tags: get java using
    Link to this answer
    Share Copy Link
    Contributed on Feb 10 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.